The document discusses promises and asynchronous programming in JavaScript. It provides examples of using promises to handle asynchronous operations and avoid callback hell. Key points include:
- Promises allow asynchronous operations to be written in a synchronous-looking way by chaining .then() and .catch() methods
- The await keyword pauses async function execution until a Promise is resolved or rejected
- Examples demonstrate promise basics like resolving, rejecting, chaining, and error handling
- Asynchronous operations like timeouts and requests still execute concurrently with promises and async/await
2. Let’s talk about JavaScript...
https://www.destroyallsoftware.com/talks/wat ;)
3. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
Polyfills:
- https://github.com/stefanpenner/es6-promise
- http://bluebirdjs.com/docs/install.html
Async / Await
Introduced: ECMAScript 2017 - June 2017
Spec: https://www.ecma-international.org/ecma-262/8.0/#sec-async-function-definitions
4. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
5. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
6. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
7. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
Polyfills:
- https://github.com/stefanpenner/es6-promise
- http://bluebirdjs.com/docs/install.html
8. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
Polyfills:
- https://github.com/stefanpenner/es6-promise
- http://bluebirdjs.com/docs/install.html
Async / Await
Introduced: ECMAScript 2017 - June 2017
Spec: https://www.ecma-international.org/ecma-262/8.0/#sec-async-function-definitions
Support: https://caniuse.com/#feat=async-functions
9. What are we even talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
Spec: https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
Polyfills:
- https://github.com/stefanpenner/es6-promise
- http://bluebirdjs.com/docs/install.html
Async / Await
Introduced: ECMAScript 2017 - June 2017
Spec: https://www.ecma-international.org/ecma-262/8.0/#sec-async-function-definitions
Support: https://caniuse.com/#feat=async-functions
10. Promise
“An object that is used as a placeholder for the eventual results of a deferred (and
possibly asynchronous) computation”
Promise p state is:
- fulfilled if p.then(f,r) enqueues a job to call f
- rejected if p.then(f,r) enqueues a job to call r
- pending if not fulfilled not rejected
- settled if not pending
- resolved if settled or locked in to match state of a different promise
62. Promise.reject(new Error(10));
VM7904:1 Uncaught (in promise) Error: 10
at <anonymous>:1:16
try {
Promise.reject(new Error(10));
} catch (error) {
console.log('caught error'); // will not be logged
}
VM7997:2 Uncaught (in promise) Error: 10
at <anonymous>:2:20
74. microtasks have priority over macrotasks
unhandled rejection can be monitored
mind handling unhandled rejections
async uses the same microtasks queue
async can execute synchronous code if not used correctly
76. For bedtime readers
Explanation of promises and async/await with examples and problems to solve
https://javascript.info/async
Async/await performance caveats:
https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need-p
romises-9b259854c161