这两个怎么不等价?
这不起作用。
const message = async () => {
setTimeout(() => {
console.log("delayed")
return true
}, 1000)
}
但这确实如此吗?
const message = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log("delayed")
resolve(true)
}, 1000)
})
}
这是否意味着您只能在异步函数中“返回”直接位于函数范围内的内容(而不是在另一个函数中)?
异步函数返回的 Promise 与常规 Promise 不同吗?它到底是如何初始化的?
最佳答案
第一个示例中的 message
函数没有 return
语句来传递其解析值。
第二个示例中的 Promise 显式地将值传递给 resolve()
。
Does this mean you can only "return" in an async function if it is directly in the scope of the function(not in another function)?
是的。函数的返回值由该函数中的return
语句确定。在其他函数中放置 return
语句将会设置该函数的返回值。
关于javascript - async 关键字/Promise 等效项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51769667/