Last active
October 8, 2018 13:38
-
-
Save rjcorwin/1f051206d0057ebb1b86877771483cd6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function a() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('a') | |
resolve() | |
}, 1000) | |
}) | |
} | |
function b() { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log('b') | |
resolve() | |
}, 1000) | |
}) | |
} | |
// Promise chaining. | |
a() | |
.then(() => { | |
return b() | |
}) | |
.then(() => { | |
return a() | |
}) | |
.then(() => { | |
return b() | |
}) | |
.then(() => console.log('done')) | |
// Compact example of Promise chaining. The return is implied and you don't have to use curly braces (also true when doing Array.map/reduce/etc). | |
a() | |
.then(() => b()) | |
.then(() => a()) | |
.then(() => b()) | |
.then(() => console.log('done')) | |
// Using async/await. | |
async function go() { | |
await b() | |
await a() | |
await b() | |
console.log('done') | |
} | |
go() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment