我正在尝试创建一个返回值但不工作的 thenable 对象:
const fetchItem = () => 'item';
function test() {
return {
async then() {
const item = await fetchItem()
return item
}
}
}
test()
.then(console.log)
then 被调用,但 console.log 不是。有什么想法吗?
最佳答案
.then
应该是一个接受回调作为参数的函数 - 你的 then
定义没有。
function test() {
return {
async then(callback) {
const item = await '3';
return callback(item);
}
}
}
test()
.then(console.log)
.then(() => console.log('done'));
关于javascript - 如何返回真正有效的 thenable 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50672142/