Share
Embed
Transcript
22 slides
-
Roadmap • Event Loop • Callbacks • Promises (ES6) •
Async functions (ES7) @di3goleite -
Resumo sobre Event Loop • JavaScript runtime é single thread
One thread === Single call stack === One thing at a time @di3goleite -
Callbacks In JavaScript - Functions are 1st class objects -
Functions can be stored in variables, passed as arguments to functions, created within functions and returned from functions. Because functions are first class objects, we can pass a function as an argument in another function and later execute that passed-in function. $('#btn_1').click(function () { alert('HUEHUE BRBR'); }); var showAlert = function () { alert('HUEHUE BRBR'); }; $('#btn_1').click(showAlert); @di3goleite -
Callback hell getMoreData(a, function(b) { getMoreData(b, function(c) { getMoreData(c, function(d)
{ getMoreData(d, function(e) { console.log('Callback Hell'); }); }); }); }); Callbacks === Continuations @di3goleite -
Callback hell - how to solve? setTimeout(function () { console.log('one');
setTimeout(function () { console.log('two'); setTimeout(function () { console.log('three'); }, 1000); }, 1000); }, 1000); function one(callback) { console.log('one'); setTimeout(callback, 1000); } function two(callback) { console.log('two'); setTimeout(callback, 1000); } function three(callback) { console.log('three'); setTimeout(callback, 1000); } one(function() { two(three); }); @di3goleite -
What are promises? A reference for a value that will
eventually become available @di3goleite -
Promises - top to bottom // There can be one
or more then() method calls that // don't provide an error handler. Then the error is // passed on until there is an error handler asyncFunction1() .then(asyncFunction2) .then(asyncFunction3) .catch(function (error) { // Something went wrong above }); Promises can be chained. This is one of the fundamental point to understand it. @di3goleite -
Promises - how that works inside? let promise = new
Promise(function(resolve, reject) { resolve("done"); reject(new Error("…")); // ignored setTimeout(() => resolve("…")); // ignored }); @di3goleite -
Async functions - how to use that? function doAsyncOperation() {
return asyncOperation().then(function(val)) { console.log(val); return val; }); } async function doAsyncOperation() { var val = await asyncOperation(); console.log(val); return val; } @di3goleite -
Async functions - chaining function doAsyncOperation() { return asyncOperation().then(function(val) {
return asyncOperation(val); }).then(function(val) { return asyncOperation(val); }).then(function(val) { return asyncOperation(val); }); } async function doAsyncOperation() { var val = await asyncOperation(); val = await asyncOperation(val); val = await asyncOperation(val); return await asyncOperation(val); } @di3goleite -
Async functions - handling errors function doAsyncOperation() { return asyncOperation().then(function(val)
{ return asyncOperation(val); }).then(function(val) { return asyncOperation(val); }).catch(function(error) { console.error(error); }); } async function doAsyncOperation() { try { var val = await asyncOperation(); val = await asyncOperation(val); return await asyncOperation(val); } catch (error) { console.error(error); } } @di3goleite