SlideShare a Scribd company logo
1 of 68
Download to read offline
A Promise is a Promise
Mateusz Bryła
Mateusz Bryła
Tech Lead at Cloudinary
Agenda
● Promises 101
● Promises caveats
● Recap
● Alternatives
● Q & A
Win a free ticket! =D
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
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
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
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
function executeDelayed(operation, callback) {
setTimeout(() => {
try {
const result = operation();
callback(null, result);
} catch (error) {
callback(error);
}
}, 1000);
}
function executeDelayed(operation, callback) {
setTimeout(() => {
try {
const result = operation();
callback(null, result);
} catch (error) {
callback(error);
}
}, 1000);
}
executeDelayed(() => {
return 42;
}, (error, result) => {
if (error) {
console.log('delayed error', error);
} else {
console.error('delayed result', result);
}
});
const promise = new Promise((resolve, reject) => {
// perform action, call resolve / reject upon completion / error
});
promise.then((result) => {
// listen for completion
});
promise.then(
(result) => {}, // listen for completion
(error) => {} // listen for error
);
promise.catch((error) => {
// listen for error
});
promise.finally(() => {
// cleanup
});
function executeDelayed(operation, callback) {
setTimeout(() => {
try {
const result = operation();
callback(null, result);
} catch (error) {
callback(error);
}
}, 1000);
}
function executeDelayed(operation) {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const result = operation();
resolve(result);
} catch (error) {
reject(error);
}
}, 1000);
});
}
function executeDelayed(operation, callback) {
setTimeout(() => {
try {
const result = operation();
callback(null, result);
} catch (error) {
callback(error);
}
}, 1000);
}
executeDelayed(() => {
return 42;
}, (error, result) => {
if (error) {
console.log('delayed error', error);
} else {
console.error('delayed result', result);
}
});
function executeDelayed(operation) {
return new Promise((resolve, reject) => {
setTimeout(() => {
try {
const result = operation();
resolve(result);
} catch (error) {
reject(error);
}
}, 1000);
});
}
executeDelayed(() => {
return 42;
}).then((result) => {
console.log('delayed result, result);
}).catch((error) => {
console.error('delayed error', error);
});
Kahoot #1
kahoot.com
p = new Promise((resolve, reject) => {
resolve(10);
resolve(20);
}).then(x => console.log(x));
p = new Promise((resolve, reject) => {
resolve(10);
resolve(20);
}).then(x => console.log(x));
// 10
p = new Promise((resolve, reject) => {
reject(10);
reject(20);
}).catch(x => console.log(x));
p = new Promise((resolve, reject) => {
reject(10);
reject(20);
}).catch(x => console.log(x));
// 10
p = new Promise((resolve, reject) => {
resolve(10);
reject(20);
}).then(x => console.log(x))
.catch(x => console.log(x));
p = new Promise((resolve, reject) => {
resolve(10);
reject(20);
}).then(x => console.log(x))
.catch(x => console.log(x));
// 10
p = new Promise((resolve, reject) => {
reject(10);
resolve(20);
}).then(x => console.log(x))
.catch(x => console.log(x));
p = new Promise((resolve, reject) => {
reject(10);
resolve(20);
}).then(x => console.log(x))
.catch(x => console.log(x));
// 10
Promise.resolve(10);
new Promise((resolve) => { resolve(10); });
Promise.reject(10);
new Promise((resolve, reject) => { reject(10); });
// fulfills if all fulfilled, rejected otherwise
Promise.all([Promise.resolve(10), Promise.reject(10)]);
// resolves as first resolves
Promise.race([Promise.resolve(10), Promise.reject(10)]);
Kahoot #2
kahoot.com
Promise.resolve(10)
.then(x => console.log(x))
.then(x => console.log(x))
.then(x => console.log(x));
// 10 undefined undefined
p = Promise.resolve(10);
p.then(x => console.log(x));
p.then(x => console.log(x));
p.then(x => console.log(x));
p = Promise.resolve(10);
p.then(x => console.log(x));
p.then(x => console.log(x));
p.then(x => console.log(x));
// 10 10 10
Promise.resolve(10)
.then(x => { console.log(x); return 20; })
.then(x => { console.log(x); return 30; })
.then(x => { console.log(x); });
// 10 20 30
Promise.resolve(10)
.then(x => { console.log(x); return 20; })
.catch(x => { console.log(x); return 30; })
.then(x => { console.log(x); return 40; })
.then(x => { console.log(x); });
Promise.resolve(10)
.then(x => { console.log(x); return 20; })
.catch(x => { console.log(x); return 30; })
.then(x => { console.log(x); return 40; })
.then(x => { console.log(x); });
// 10 20 40
Kahoot #3
kahoot.com
Promise.reject(10)
.catch(x => { console.log(x); return 20; })
.then(x => { console.log(x); return 30; })
.catch(x => { console.log(x); return 40; })
.then(x => { console.log(x); });
// 10 20 30
Promise.resolve(10)
.then(x => { console.log(x); return 20; })
.then(x => { console.log(x); throw new Error(30); })
.then(x => { console.log(x); return 40; })
.catch(x => { console.log(x); return 50; });
Promise.resolve(10)
.then(x => { console.log(x); return 20; })
.then(x => { console.log(x); throw new Error(30); })
.then(x => { console.log(x); return 40; })
.catch(x => { console.log(x); return 50; });
// 10 20 Error(30)
reject(new Error(10))
Promise.reject(10)
.catch(x => console.log(x))
.catch(x => console.log(x))
.catch(x => console.log(x));
Promise.reject(10)
.catch(x => console.log(x))
.catch(x => console.log(x))
.catch(x => console.log(x));
// 10
Kahoot #4
kahoot.com
Promise.resolve(10)
.then(x => { console.log(x); return Promise.resolve(20); })
.then(x => { console.log(x); });
// 10 20
fetch('https://api.com/users/42')
.then(response => response.json())
.then(user => fetch(`https://api.github.com/users/${user.name}`))
.then(response => response.json())
.then(user => new Promise((resolve, reject) => {
// check out users projects
resolve();
}));
Promise.all([
Promise.resolve(10),
20,
Promise.resolve(30),
]);
Promise.all([
Promise.resolve(10),
20,
Promise.resolve(30),
]);
// [10 20 30]
promises keep state
be mindful of promises chaining
catch returns fulfilled promise if error not rethrown
Kahoot #5
kahoot.com
Promise.resolve(10).then(x => {
console.log(x);
return 20;
}).then(x => console.log(x));
Promise.resolve(30).then(x => {
console.log(x);
return 40;
}).then(x => console.log(x));
// 10 30 20 40
Promise.resolve(10).then(x => {
console.log(x);
return Promise.resolve(20);
}).then(x => console.log(x));
Promise.resolve(30).then(x => {
console.log(x);
return 40;
}).then(x => console.log(x));
Promise.resolve(10).then(x => {
console.log(x);
return Promise.resolve(20);
}).then(x => console.log(x));
Promise.resolve(30).then(x => {
console.log(x);
return 40;
}).then(x => console.log(x));
// 10 30 40 20
setTimeout(() => console.log('macro'), 0);
Promise.resolve().then(() => console.log('micro'));
console.log('current');
setTimeout(() => console.log('macro'), 0);
Promise.resolve().then(() => console.log('micro'));
console.log('current');
// current micro macro
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
window.addEventListener('unhandledrejection', (event) => {
console.log('unhandled', event.reason);
});
p = new Promise(() => {
throw new Error(20);
});
// unhandled Error(20)
window.addEventListener('unhandledrejection', (event) => {
console.log('unhandled', event.reason);
});
p = new Promise(() => {
throw new Error(20);
});
setTimeout(() => {
p.catch(e => console.log('handling', e));
}, 1000);
// unhandled Error(20)
// handling Error(20)
window.addEventListener('unhandledrejection', (event) => {
console.log('unhandled', event.reason); // 1st
});
window.addEventListener('rejectionhandled', (event) => {
console.log('handled rejected', event.reason); // 5th
});
p = new Promise(() => {
throw new Error(20);
});
setTimeout(() => {
console.log('will handle'); // 2nd
p.catch(e => console.log('handling', e)); // 4th
console.log('handled'); // 3rd
}, 1000);
async function foo() {
return 1;
}
foo().then(x => console.log(x));
// 1
async function foo() {
return Promise.resolve(1);
}
foo().then(x => console.log(x));
// 1
async function foo() {
return 1;
}
foo().then(x => console.log(x));
// 1
async function foo() {
console.log('before');
const result = await p();
console.log('after', result);
}
async function foo() {
return Promise.resolve(1);
}
foo().then(x => console.log(x));
// 1
async function foo() {
console.log('before');
const result = await p();
console.log('after', result);
}
Kahoot #6
kahoot.com
(async () => {
setTimeout(() => console.log('timeout'), 0);
await f(); // another async function, execution duration: 5s
console.log('after await');
})();
// after await timeout
async function prepareDinner(meatType = 'fish') {
const dinner = await prepareBaseDinner();
const meat = await selectMeat(meatType);
const wine = await selectWine(meat.getWineType());
dinner.addMeat(meat);
dinner.addWine(wine);
return dinner;
}
async function prepareDinner(meatType = 'fish') {
const dinner = await prepareBaseDinner();
const meat = await selectMeat(meatType);
const wine = await selectWine(meat.getWineType());
dinner.addMeat(meat);
dinner.addWine(wine);
return dinner;
}
// |-------dinner------->|---meat--->|---wine--->|
function prepareDinner(meatType = 'fish') {
const dinnerPromise = prepareBaseDinner();
const meatPromise = selectMeat(meatType);
const winePromise = meatPromise.then(meat => {
return selectWine(meat.getWineType());
});
return Promise.all([ dinnerPromise, meatPromise, winePromise ]).then(
([ dinner, meat, wine ]) => {
dinner.addMeat(meat);
dinner.addWine(wine);
return dinner;
}
);
}
function prepareDinner(meatType = 'fish') {
const dinnerPromise = prepareBaseDinner();
const meatPromise = selectMeat(meatType);
const winePromise = meatPromise.then(meat => {
return selectWine(meat.getWineType());
});
return Promise.all([ dinnerPromise, meatPromise, winePromise ]).then(
([ dinner, meat, wine ]) => {
dinner.addMeat(meat);
dinner.addWine(wine);
return dinner;
}
);
}
// |-------dinner------->|
// |---meat--->|---wine--->|
microtasks have priority over macrotasks
unhandled rejection can be monitored
mind handling unhandled rejections
async can execute synchronous code if not used correctly
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/
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
Kahoot Finals
kahoot.com
For bedtime readers ;)
Promises and async / await - examples and problems
https://javascript.info/async
Async / await - performance caveats
https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need-
promises-9b259854c161
Tasks (and inner links to good RxJS article)
https://medium.com/@yannickdot/taming-the-async-beast-using-tasks-e62675dde
9c
Q & A
sli.do - #sphereit - JSSphere
Thank you!
A Promise is a Promise - Mateusz Bryła
A promise is a Promise

More Related Content

What's hot

The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 74 of 202
The Ring programming language version 1.8 book - Part 74 of 202The Ring programming language version 1.8 book - Part 74 of 202
The Ring programming language version 1.8 book - Part 74 of 202Mahmoud Samir Fayed
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScriptSam Cartwright
 
The Ring programming language version 1.7 book - Part 72 of 196
The Ring programming language version 1.7 book - Part 72 of 196The Ring programming language version 1.7 book - Part 72 of 196
The Ring programming language version 1.7 book - Part 72 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 18 of 30
The Ring programming language version 1.4 book - Part 18 of 30The Ring programming language version 1.4 book - Part 18 of 30
The Ring programming language version 1.4 book - Part 18 of 30Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 50 of 88
The Ring programming language version 1.3 book - Part 50 of 88The Ring programming language version 1.3 book - Part 50 of 88
The Ring programming language version 1.3 book - Part 50 of 88Mahmoud Samir Fayed
 
The Ring programming language version 1.5.1 book - Part 57 of 180
The Ring programming language version 1.5.1 book - Part 57 of 180The Ring programming language version 1.5.1 book - Part 57 of 180
The Ring programming language version 1.5.1 book - Part 57 of 180Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.9 book - Part 62 of 210The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.9 book - Part 62 of 210Mahmoud Samir Fayed
 
The Ring programming language version 1.4 book - Part 16 of 30
The Ring programming language version 1.4 book - Part 16 of 30The Ring programming language version 1.4 book - Part 16 of 30
The Ring programming language version 1.4 book - Part 16 of 30Mahmoud Samir Fayed
 
The Ring programming language version 1.7 book - Part 73 of 196
The Ring programming language version 1.7 book - Part 73 of 196The Ring programming language version 1.7 book - Part 73 of 196
The Ring programming language version 1.7 book - Part 73 of 196Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 75 of 202
The Ring programming language version 1.8 book - Part 75 of 202The Ring programming language version 1.8 book - Part 75 of 202
The Ring programming language version 1.8 book - Part 75 of 202Mahmoud Samir Fayed
 
The Ring programming language version 1.5.3 book - Part 77 of 184
The Ring programming language version 1.5.3 book - Part 77 of 184The Ring programming language version 1.5.3 book - Part 77 of 184
The Ring programming language version 1.5.3 book - Part 77 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 45 of 84
The Ring programming language version 1.2 book - Part 45 of 84The Ring programming language version 1.2 book - Part 45 of 84
The Ring programming language version 1.2 book - Part 45 of 84Mahmoud Samir Fayed
 
The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88Mahmoud Samir Fayed
 

What's hot (20)

The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189The Ring programming language version 1.6 book - Part 55 of 189
The Ring programming language version 1.6 book - Part 55 of 189
 
The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196The Ring programming language version 1.7 book - Part 71 of 196
The Ring programming language version 1.7 book - Part 71 of 196
 
The Ring programming language version 1.8 book - Part 74 of 202
The Ring programming language version 1.8 book - Part 74 of 202The Ring programming language version 1.8 book - Part 74 of 202
The Ring programming language version 1.8 book - Part 74 of 202
 
Making Games in JavaScript
Making Games in JavaScriptMaking Games in JavaScript
Making Games in JavaScript
 
The Ring programming language version 1.7 book - Part 72 of 196
The Ring programming language version 1.7 book - Part 72 of 196The Ring programming language version 1.7 book - Part 72 of 196
The Ring programming language version 1.7 book - Part 72 of 196
 
The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210The Ring programming language version 1.9 book - Part 60 of 210
The Ring programming language version 1.9 book - Part 60 of 210
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
The Ring programming language version 1.4 book - Part 18 of 30
The Ring programming language version 1.4 book - Part 18 of 30The Ring programming language version 1.4 book - Part 18 of 30
The Ring programming language version 1.4 book - Part 18 of 30
 
The Ring programming language version 1.3 book - Part 50 of 88
The Ring programming language version 1.3 book - Part 50 of 88The Ring programming language version 1.3 book - Part 50 of 88
The Ring programming language version 1.3 book - Part 50 of 88
 
Corona sdk
Corona sdkCorona sdk
Corona sdk
 
The Ring programming language version 1.5.1 book - Part 57 of 180
The Ring programming language version 1.5.1 book - Part 57 of 180The Ring programming language version 1.5.1 book - Part 57 of 180
The Ring programming language version 1.5.1 book - Part 57 of 180
 
The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.9 book - Part 62 of 210The Ring programming language version 1.9 book - Part 62 of 210
The Ring programming language version 1.9 book - Part 62 of 210
 
The Ring programming language version 1.4 book - Part 16 of 30
The Ring programming language version 1.4 book - Part 16 of 30The Ring programming language version 1.4 book - Part 16 of 30
The Ring programming language version 1.4 book - Part 16 of 30
 
The Ring programming language version 1.7 book - Part 73 of 196
The Ring programming language version 1.7 book - Part 73 of 196The Ring programming language version 1.7 book - Part 73 of 196
The Ring programming language version 1.7 book - Part 73 of 196
 
The Ring programming language version 1.8 book - Part 75 of 202
The Ring programming language version 1.8 book - Part 75 of 202The Ring programming language version 1.8 book - Part 75 of 202
The Ring programming language version 1.8 book - Part 75 of 202
 
The Ring programming language version 1.5.3 book - Part 77 of 184
The Ring programming language version 1.5.3 book - Part 77 of 184The Ring programming language version 1.5.3 book - Part 77 of 184
The Ring programming language version 1.5.3 book - Part 77 of 184
 
project3
project3project3
project3
 
The Ring programming language version 1.2 book - Part 45 of 84
The Ring programming language version 1.2 book - Part 45 of 84The Ring programming language version 1.2 book - Part 45 of 84
The Ring programming language version 1.2 book - Part 45 of 84
 
Include
IncludeInclude
Include
 
The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88The Ring programming language version 1.3 book - Part 40 of 88
The Ring programming language version 1.3 book - Part 40 of 88
 

Similar to A promise is a Promise

Better react/redux apps using redux-saga
Better react/redux apps using redux-sagaBetter react/redux apps using redux-saga
Better react/redux apps using redux-sagaYounes (omar) Meliani
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJSKyung Yeol Kim
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency명신 김
 
Promise: async programming hero
Promise: async programming heroPromise: async programming hero
Promise: async programming heroThe Software House
 
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018Codemotion
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6Dmitry Soshnikov
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de donnéesRomain Lecomte
 
$q and Promises in AngularJS
$q and Promises in AngularJS $q and Promises in AngularJS
$q and Promises in AngularJS a_sharif
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in GolangBo-Yi Wu
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Oscar Renalias
 
Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Kim Hunmin
 
Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ TwitterC4Media
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Domenic Denicola
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feelingErin Zimmer
 
The Ring programming language version 1.5.1 book - Part 63 of 180
The Ring programming language version 1.5.1 book - Part 63 of 180The Ring programming language version 1.5.1 book - Part 63 of 180
The Ring programming language version 1.5.1 book - Part 63 of 180Mahmoud Samir Fayed
 

Similar to A promise is a Promise (20)

Better react/redux apps using redux-saga
Better react/redux apps using redux-sagaBetter react/redux apps using redux-saga
Better react/redux apps using redux-saga
 
ECMAScript 2015 Tips & Traps
ECMAScript 2015 Tips & TrapsECMAScript 2015 Tips & Traps
ECMAScript 2015 Tips & Traps
 
Compose Async with RxJS
Compose Async with RxJSCompose Async with RxJS
Compose Async with RxJS
 
C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Promise: async programming hero
Promise: async programming heroPromise: async programming hero
Promise: async programming hero
 
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018
Pablo Magaz | ECMAScript 2018 y más allá | Codemotion Madrid 2018
 
FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6FalsyValues. Dmitry Soshnikov - ECMAScript 6
FalsyValues. Dmitry Soshnikov - ECMAScript 6
 
Un dsl pour ma base de données
Un dsl pour ma base de donnéesUn dsl pour ma base de données
Un dsl pour ma base de données
 
$q and Promises in AngularJS
$q and Promises in AngularJS $q and Promises in AngularJS
$q and Promises in AngularJS
 
Job Queue in Golang
Job Queue in GolangJob Queue in Golang
Job Queue in Golang
 
Angular2 rxjs
Angular2 rxjsAngular2 rxjs
Angular2 rxjs
 
Monadologie
MonadologieMonadologie
Monadologie
 
Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0Asynchronous web apps with the Play Framework 2.0
Asynchronous web apps with the Play Framework 2.0
 
Lexical environment in ecma 262 5
Lexical environment in ecma 262 5Lexical environment in ecma 262 5
Lexical environment in ecma 262 5
 
Mutation @ Spotify
Mutation @ Spotify Mutation @ Spotify
Mutation @ Spotify
 
Functional Systems @ Twitter
Functional Systems @ TwitterFunctional Systems @ Twitter
Functional Systems @ Twitter
 
Coding in Style
Coding in StyleCoding in Style
Coding in Style
 
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
Callbacks, Promises, and Coroutines (oh my!): Asynchronous Programming Patter...
 
Async Testing giving you a sinking feeling
Async Testing giving you a sinking feelingAsync Testing giving you a sinking feeling
Async Testing giving you a sinking feeling
 
The Ring programming language version 1.5.1 book - Part 63 of 180
The Ring programming language version 1.5.1 book - Part 63 of 180The Ring programming language version 1.5.1 book - Part 63 of 180
The Ring programming language version 1.5.1 book - Part 63 of 180
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....ShaimaaMohamedGalal
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 

Recently uploaded (20)

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Clustering techniques data mining book ....
Clustering techniques data mining book ....Clustering techniques data mining book ....
Clustering techniques data mining book ....
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 

A promise is a Promise

  • 1. A Promise is a Promise Mateusz Bryła
  • 2. Mateusz Bryła Tech Lead at Cloudinary
  • 3. Agenda ● Promises 101 ● Promises caveats ● Recap ● Alternatives ● Q & A Win a free ticket! =D
  • 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
  • 8. function executeDelayed(operation, callback) { setTimeout(() => { try { const result = operation(); callback(null, result); } catch (error) { callback(error); } }, 1000); }
  • 9. function executeDelayed(operation, callback) { setTimeout(() => { try { const result = operation(); callback(null, result); } catch (error) { callback(error); } }, 1000); } executeDelayed(() => { return 42; }, (error, result) => { if (error) { console.log('delayed error', error); } else { console.error('delayed result', result); } });
  • 10. const promise = new Promise((resolve, reject) => { // perform action, call resolve / reject upon completion / error }); promise.then((result) => { // listen for completion }); promise.then( (result) => {}, // listen for completion (error) => {} // listen for error ); promise.catch((error) => { // listen for error }); promise.finally(() => { // cleanup });
  • 11. function executeDelayed(operation, callback) { setTimeout(() => { try { const result = operation(); callback(null, result); } catch (error) { callback(error); } }, 1000); } function executeDelayed(operation) { return new Promise((resolve, reject) => { setTimeout(() => { try { const result = operation(); resolve(result); } catch (error) { reject(error); } }, 1000); }); }
  • 12. function executeDelayed(operation, callback) { setTimeout(() => { try { const result = operation(); callback(null, result); } catch (error) { callback(error); } }, 1000); } executeDelayed(() => { return 42; }, (error, result) => { if (error) { console.log('delayed error', error); } else { console.error('delayed result', result); } }); function executeDelayed(operation) { return new Promise((resolve, reject) => { setTimeout(() => { try { const result = operation(); resolve(result); } catch (error) { reject(error); } }, 1000); }); } executeDelayed(() => { return 42; }).then((result) => { console.log('delayed result, result); }).catch((error) => { console.error('delayed error', error); });
  • 14. p = new Promise((resolve, reject) => { resolve(10); resolve(20); }).then(x => console.log(x));
  • 15. p = new Promise((resolve, reject) => { resolve(10); resolve(20); }).then(x => console.log(x)); // 10
  • 16. p = new Promise((resolve, reject) => { reject(10); reject(20); }).catch(x => console.log(x));
  • 17. p = new Promise((resolve, reject) => { reject(10); reject(20); }).catch(x => console.log(x)); // 10
  • 18. p = new Promise((resolve, reject) => { resolve(10); reject(20); }).then(x => console.log(x)) .catch(x => console.log(x));
  • 19. p = new Promise((resolve, reject) => { resolve(10); reject(20); }).then(x => console.log(x)) .catch(x => console.log(x)); // 10
  • 20. p = new Promise((resolve, reject) => { reject(10); resolve(20); }).then(x => console.log(x)) .catch(x => console.log(x));
  • 21. p = new Promise((resolve, reject) => { reject(10); resolve(20); }).then(x => console.log(x)) .catch(x => console.log(x)); // 10
  • 22. Promise.resolve(10); new Promise((resolve) => { resolve(10); }); Promise.reject(10); new Promise((resolve, reject) => { reject(10); }); // fulfills if all fulfilled, rejected otherwise Promise.all([Promise.resolve(10), Promise.reject(10)]); // resolves as first resolves Promise.race([Promise.resolve(10), Promise.reject(10)]);
  • 24. Promise.resolve(10) .then(x => console.log(x)) .then(x => console.log(x)) .then(x => console.log(x)); // 10 undefined undefined
  • 25. p = Promise.resolve(10); p.then(x => console.log(x)); p.then(x => console.log(x)); p.then(x => console.log(x));
  • 26. p = Promise.resolve(10); p.then(x => console.log(x)); p.then(x => console.log(x)); p.then(x => console.log(x)); // 10 10 10
  • 27. Promise.resolve(10) .then(x => { console.log(x); return 20; }) .then(x => { console.log(x); return 30; }) .then(x => { console.log(x); }); // 10 20 30
  • 28. Promise.resolve(10) .then(x => { console.log(x); return 20; }) .catch(x => { console.log(x); return 30; }) .then(x => { console.log(x); return 40; }) .then(x => { console.log(x); });
  • 29. Promise.resolve(10) .then(x => { console.log(x); return 20; }) .catch(x => { console.log(x); return 30; }) .then(x => { console.log(x); return 40; }) .then(x => { console.log(x); }); // 10 20 40
  • 31. Promise.reject(10) .catch(x => { console.log(x); return 20; }) .then(x => { console.log(x); return 30; }) .catch(x => { console.log(x); return 40; }) .then(x => { console.log(x); }); // 10 20 30
  • 32. Promise.resolve(10) .then(x => { console.log(x); return 20; }) .then(x => { console.log(x); throw new Error(30); }) .then(x => { console.log(x); return 40; }) .catch(x => { console.log(x); return 50; });
  • 33. Promise.resolve(10) .then(x => { console.log(x); return 20; }) .then(x => { console.log(x); throw new Error(30); }) .then(x => { console.log(x); return 40; }) .catch(x => { console.log(x); return 50; }); // 10 20 Error(30)
  • 35. Promise.reject(10) .catch(x => console.log(x)) .catch(x => console.log(x)) .catch(x => console.log(x));
  • 36. Promise.reject(10) .catch(x => console.log(x)) .catch(x => console.log(x)) .catch(x => console.log(x)); // 10
  • 38. Promise.resolve(10) .then(x => { console.log(x); return Promise.resolve(20); }) .then(x => { console.log(x); }); // 10 20
  • 39. fetch('https://api.com/users/42') .then(response => response.json()) .then(user => fetch(`https://api.github.com/users/${user.name}`)) .then(response => response.json()) .then(user => new Promise((resolve, reject) => { // check out users projects resolve(); }));
  • 42. promises keep state be mindful of promises chaining catch returns fulfilled promise if error not rethrown
  • 44. Promise.resolve(10).then(x => { console.log(x); return 20; }).then(x => console.log(x)); Promise.resolve(30).then(x => { console.log(x); return 40; }).then(x => console.log(x)); // 10 30 20 40
  • 45. Promise.resolve(10).then(x => { console.log(x); return Promise.resolve(20); }).then(x => console.log(x)); Promise.resolve(30).then(x => { console.log(x); return 40; }).then(x => console.log(x));
  • 46. Promise.resolve(10).then(x => { console.log(x); return Promise.resolve(20); }).then(x => console.log(x)); Promise.resolve(30).then(x => { console.log(x); return 40; }).then(x => console.log(x)); // 10 30 40 20
  • 47. setTimeout(() => console.log('macro'), 0); Promise.resolve().then(() => console.log('micro')); console.log('current');
  • 48. setTimeout(() => console.log('macro'), 0); Promise.resolve().then(() => console.log('micro')); console.log('current'); // current micro macro
  • 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
  • 50. window.addEventListener('unhandledrejection', (event) => { console.log('unhandled', event.reason); }); p = new Promise(() => { throw new Error(20); }); // unhandled Error(20)
  • 51. window.addEventListener('unhandledrejection', (event) => { console.log('unhandled', event.reason); }); p = new Promise(() => { throw new Error(20); }); setTimeout(() => { p.catch(e => console.log('handling', e)); }, 1000); // unhandled Error(20) // handling Error(20)
  • 52. window.addEventListener('unhandledrejection', (event) => { console.log('unhandled', event.reason); // 1st }); window.addEventListener('rejectionhandled', (event) => { console.log('handled rejected', event.reason); // 5th }); p = new Promise(() => { throw new Error(20); }); setTimeout(() => { console.log('will handle'); // 2nd p.catch(e => console.log('handling', e)); // 4th console.log('handled'); // 3rd }, 1000);
  • 53. async function foo() { return 1; } foo().then(x => console.log(x)); // 1 async function foo() { return Promise.resolve(1); } foo().then(x => console.log(x)); // 1
  • 54. async function foo() { return 1; } foo().then(x => console.log(x)); // 1 async function foo() { console.log('before'); const result = await p(); console.log('after', result); } async function foo() { return Promise.resolve(1); } foo().then(x => console.log(x)); // 1 async function foo() { console.log('before'); const result = await p(); console.log('after', result); }
  • 56. (async () => { setTimeout(() => console.log('timeout'), 0); await f(); // another async function, execution duration: 5s console.log('after await'); })(); // after await timeout
  • 57. async function prepareDinner(meatType = 'fish') { const dinner = await prepareBaseDinner(); const meat = await selectMeat(meatType); const wine = await selectWine(meat.getWineType()); dinner.addMeat(meat); dinner.addWine(wine); return dinner; }
  • 58. async function prepareDinner(meatType = 'fish') { const dinner = await prepareBaseDinner(); const meat = await selectMeat(meatType); const wine = await selectWine(meat.getWineType()); dinner.addMeat(meat); dinner.addWine(wine); return dinner; } // |-------dinner------->|---meat--->|---wine--->|
  • 59. function prepareDinner(meatType = 'fish') { const dinnerPromise = prepareBaseDinner(); const meatPromise = selectMeat(meatType); const winePromise = meatPromise.then(meat => { return selectWine(meat.getWineType()); }); return Promise.all([ dinnerPromise, meatPromise, winePromise ]).then( ([ dinner, meat, wine ]) => { dinner.addMeat(meat); dinner.addWine(wine); return dinner; } ); }
  • 60. function prepareDinner(meatType = 'fish') { const dinnerPromise = prepareBaseDinner(); const meatPromise = selectMeat(meatType); const winePromise = meatPromise.then(meat => { return selectWine(meat.getWineType()); }); return Promise.all([ dinnerPromise, meatPromise, winePromise ]).then( ([ dinner, meat, wine ]) => { dinner.addMeat(meat); dinner.addWine(wine); return dinner; } ); } // |-------dinner------->| // |---meat--->|---wine--->|
  • 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
  • 65. For bedtime readers ;) Promises and async / await - examples and problems https://javascript.info/async Async / await - performance caveats https://medium.com/@bluepnume/even-with-async-await-you-probably-still-need- promises-9b259854c161 Tasks (and inner links to good RxJS article) https://medium.com/@yannickdot/taming-the-async-beast-using-tasks-e62675dde 9c
  • 66. Q & A sli.do - #sphereit - JSSphere
  • 67. Thank you! A Promise is a Promise - Mateusz Bryła