【发布时间】:2018-12-01 21:49:33
【问题描述】:
我想知道哪个代码更有意义,如果 promise 不包含值,那么返回这样的 promise 是否有任何意义:
async function action () {
// let's say there are many such lines with await before returning
await asyncFunction()
// will return Promise that does not contain any value!
return anotherAsyncFunction()
}
// action().then(myAnotherAction)
或者这样做更明智:
async function action () {
await asyncFunction()
await anotherAsyncFunction()
}
// const result = await action()
第一个选项是只在返回一些值时使用?
否则,使用第二个选项更容易,因为在函数末尾添加另一个带有“等待”的动作更容易?
async function action () {
await asyncFunction()
await anotherAsyncFunction()
// easy to add another function, no need to touch "return"
await andAnotherAsyncFunction()
}
【问题讨论】:
标签: javascript promise async-await