Skip to content

Instantly share code, notes, and snippets.

@mygoare
Last active April 18, 2018 11:18
Show Gist options
  • Save mygoare/40426d3d811237d22f3475160d6770b0 to your computer and use it in GitHub Desktop.
Save mygoare/40426d3d811237d22f3475160d6770b0 to your computer and use it in GitHub Desktop.
JS Bin// source http://jsbin.com/yiyemen
// 理解 async / await
// async function 返回的都是promise<pending>
// 需要 await 接 promise ,得到的才会是 promise后的值, 但 await 必须要出现在 async 函数中
// 所以 async 函数 可以作为 返回值使用(赋值给一个变量),但仍然需要 在另一个 async函数中 await 后调用
function resolveAfter2Seconds() {
return new Promise(resolve => {
setTimeout(() => {
resolve('resolved');
}, 2000);
});
}
let result = async function asyncCall() {
console.log('calling');
return result = await resolveAfter2Seconds();
}
async function tt() {
let re = await result()
console.log(re)
}
tt()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment