These slides have been presented in a lecture about promises at the SphereIT conference on Octobere 19th 2019.
Author: Mateusz Bryła (https://www.linkedin.com/in/mateusz-bryła-78aa1572/)
Dreamer, programmer, consultant, trainer. Founder at Lingmates, Team Lead at Codete.
4. What are we talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
https://www.ecma-international.org/ecma-262/6.0/#sec-promise-objects
Support: https://caniuse.com/#feat=promises
5. What are we talking about?
Promise Objects
Introduced: ECMAScript 2015 (previously ES6) - June 2015
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
• https://polyfill.io
6. What are we talking about?
Async / await
Introduced: ECMAScript 2017 - June 2017
https://www.ecma-international.org/ecma-262/8.0/#sec-async-function-definitions
Support: https://caniuse.com/#feat=async-functions
7. A Promise
“An object that is used as a placeholder for the eventual results of a deferred (and
possibly asynchronous) computation”ECMAScript specification
Three mutually exclusive states: fulfilled, rejected and pending.
• 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 is neither fulfilled nor rejected
• settled if not pending
• resolved if settled or locked in to match state of a different promise
49. 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
61. microtasks have priority over macrotasks
unhandled rejection can be monitored
mind handling unhandled rejections
async can execute synchronous code if not used correctly
62. Are there alternatives?
Tasks
data.task - https://github.com/folktale/data.task
Fluture - https://github.com/fluture-js/Fluture
RxJS
https://rxjs-dev.firebaseapp.com/
63. What are the differences?
Promises Tasks RxJS
eager lazy lazy
not cancellable cancellable cancellable
multiple observers, single
return value
single observer, single return
value
multiple observers, multiple
return values