Created
May 3, 2017 00:58
-
-
Save shaohua/2a99796d5860ec5dc4fd2882cc49b70d to your computer and use it in GitHub Desktop.
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 v7.9.0 */ | |
| /* v0, just async */ | |
| (async function() { | |
| var sleep = async function() { | |
| return 'done'; | |
| } | |
| var result = await sleep(); | |
| console.log('v0 result', result); | |
| })(); | |
| // /* v1, async, await */ | |
| (async function() { | |
| function timeoutPromised(ms) { | |
| return new Promise(resolve => setTimeout(() => { | |
| resolve('timeout is done'); | |
| }, ms)); | |
| } | |
| var sleep = async function(para) { | |
| var awaitResult = await timeoutPromised(para); | |
| return 'done' + awaitResult; | |
| } | |
| var result = await sleep(1000); | |
| console.log('v1 result', result); | |
| })(); | |
| // /* v2, multiple async await */ | |
| (async function() { | |
| function timeoutPromised(ms) { | |
| return new Promise(resolve => setTimeout(() => { | |
| resolve('timeout is done'); | |
| }, ms)); | |
| } | |
| var sleep = async function(para) { | |
| var awaitResult1 = await timeoutPromised(para); | |
| var awaitResult2 = await timeoutPromised(para); | |
| return 'done' + awaitResult1 + awaitResult2; | |
| } | |
| var result = await sleep(1000); | |
| console.log('v2 result', result); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment