We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
表示函数中存在异步操作
// 语法 async function name(params) { statements }
await 只能在 async 函数中使用, 表示需要等待后面的表达式返回结果
await
async
// 语法 returnValue = await expression
注意 await 后面的 Promise 有可能返回的是 rejected, 所以最好把 await 放在 try···catch 中, 方便错误处理
Promise
rejected
try···catch
try { await expression } catch (err) { console.log(err) }
function test() { return new Promise((resolve, reject) => { setTimeout(() => { console.log('status:resolve') resolve('resolve') }, 2000) }) } async function async() { console.log('asyncfn start') let result = await test() console.log(result) } async() function testFn(x) { return new Promise((resolve, reject) => { setTimeout(() => { resolve(x) }, 2000) }) } async function asyncFn(x) { let a = await testFn(10) let b = await testFn(20) console.log(`${x}+${a}+${b}`) } asyncFn(30).then(() => { console.log('asycnFn finished') })
The text was updated successfully, but these errors were encountered:
No branches or pull requests
async await
async
await
注意
await
后面的Promise
有可能返回的是rejected
, 所以最好把await
放在try···catch
中, 方便错误处理demo
The text was updated successfully, but these errors were encountered: