Skip to content

Instantly share code, notes, and snippets.

@rjcorwin
Last active October 8, 2018 13:38
Show Gist options
  • Save rjcorwin/1f051206d0057ebb1b86877771483cd6 to your computer and use it in GitHub Desktop.
Save rjcorwin/1f051206d0057ebb1b86877771483cd6 to your computer and use it in GitHub Desktop.
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