作者:暗恋达志_227 | 来源:互联网 | 2022-12-09 16:28
我正在学习承诺,因此我绝对希望在继续之前先了解它们的用法。我正在使用在线服务库,该库具有返回承诺的功能。
我读过的几乎所有示例都在链接then()
函数中使用了已解析的数据
const result = Library.functionReturningAPromise()
result.then(function(res) {
const obj = new Example(res)
return obj
}).then(function(ob) {
// do the rest of the logic within these then() functions
})
或在async
函数中使用解析的数据
async function test() {
const result = await Library.functionReturningAPromise()
const obj = new Example(result)
// do the rest of the logic
}
我想知道是否有任何方法可以使用“正常”同步代码中已解决的承诺中的数据
const result = Library.functionReturningAPromise()
// do something to resolve the promise
const obj = new Example(result)
或者,如果您需要始终“包装” 使用函数中已解决的承诺中的数据的所有逻辑async
。