Sebastian Herrmann
A software-developing peep.
Promises help us manage asynchronous operations (mainly server requests).
They provide a simple model to represent…
getAllTitles()
.then(allTitles => {...})
.catch(error => {...})
success!
failure!
📞 function call
🕑
✔️ settled
returns a promise
.finally(...)
In all of our frontend projects, of course (webapp, cms, business dashboard, audience builder, memento, …)!
.resolve(value)
.reject(error)
.all([...])
.race([...])
.then(value)
.catch(error)
.finally(result)
returns a "successful" promise result
returns a "failed" promise result
resolves when all promises are finished
resolves when any promise is finished
handle the sucessful result
handle the failed result
do something when the promise finished
(not availabe in ES6 Promises)
const myPromise = new Promise(function(resolve, reject) {
// Do an async task async task and then...
if (/* good condition */) {
resolve('Success!');
}
else {
reject('Failure!');
}
});
myPromise.then(function(result) {
/* Do something with the result. */
}).catch(function(error) {
/* Handle the error, e.g. show a message to the
user or log it to the backend, … */
});
async function getTitles() {
return fetch('https://apis.justwatch.com/content/titles/en_US/popular');
}
const titles = await getTitles();
console.log(titles);
// or getTitles().then(...);
By Sebastian Herrmann
Introduction to promises and how we're using them.