Skip to content

Instantly share code, notes, and snippets.

@shaohua
Created May 3, 2017 00:58
Show Gist options
  • Select an option

  • Save shaohua/2a99796d5860ec5dc4fd2882cc49b70d to your computer and use it in GitHub Desktop.

Select an option

Save shaohua/2a99796d5860ec5dc4fd2882cc49b70d to your computer and use it in GitHub Desktop.
async await
/* 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