Last active
October 24, 2017 08:32
-
-
Save ktanaka117/9f1c60437dd5cefbd4f513d286c269d9 to your computer and use it in GitHub Desktop.
JavaScriptのPromiseとasync/awaitの比較
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
'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