Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion JavaScript_Advance/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,22 @@ b.then((value) => {
})
.catch((value) => {
console.log(value);
});
});

// you can chain promises like so:
// Assuming we have an asynchronous call to retrieve a value like so:
const asyncCall = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("foo");
}, 2000);
});

asyncCall
// will become 'FOO'
.then(val => val.toUpperCase())
// converted back to 'foo'
.then(val => val.toLowerCase())
// appended with ' bar' to become 'foo bar'
.then(val => `${val} bar`)
// console logs 'foo bar'
.then(console.log);