Skip to content

Instantly share code, notes, and snippets.

@ktanaka117
Last active October 24, 2017 08:32
Show Gist options
  • Save ktanaka117/9f1c60437dd5cefbd4f513d286c269d9 to your computer and use it in GitHub Desktop.
Save ktanaka117/9f1c60437dd5cefbd4f513d286c269d9 to your computer and use it in GitHub Desktop.
JavaScriptのPromiseとasync/awaitの比較
'use strict'
const sleep = (someFunction) => {
setTimeout(someFunction, 5000)
}
// Promise
const promiseFunction = (() => {
return new Promise((resolve, reject) => {
sleep(() => {
resolve(console.log('Promise'))
})
})
})
// async/await
const asyncAwaitFunction = async () => {
await sleep(() => {
console.log('async/await')
})
}
const meetUp = () => {
promiseFunction().then(async () => {
await asyncAwaitFunction()
})
}
meetUp()
// Promise <- 5 seconds later.
// async/await <- 10(5) seconds later.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment