Skip to content

Instantly share code, notes, and snippets.

@tomschall
Forked from bmorelli25/async1.js
Created January 17, 2019 17:07
Show Gist options
  • Save tomschall/af0f99fb3f84bd31aa4f0f68d5e80ca3 to your computer and use it in GitHub Desktop.
Save tomschall/af0f99fb3f84bd31aa4f0f68d5e80ca3 to your computer and use it in GitHub Desktop.
promise example
function doubleAfter2Seconds(x) {
return new Promise(resolve => {
setTimeout(() => {
resolve(x * 2);
}, 2000);
});
}
function addPromise(x){
return new Promise(resolve => {
doubleAfter2Seconds(10).then((a) => {
doubleAfter2Seconds(20).then((b) => {
doubleAfter2Seconds(30).then((c) => {
resolve(x + a + b + c);
})
})
})
});
}
addPromise(10).then((sum) => {
console.log(sum);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment