我正在尝试创建一个返回值的thenable对象,但不起作用:
const fetchItem = () => 'item';
function test() {
return {
async then() {
const item = await fetchItem()
return item
}
}
}
test()
.then(console.log)
然后调用then,但是console.log不是。任何想法为什么?
答案 0 :(得分:1)
.then
应该是一个接受回调作为参数的函数 - 您的then
定义没有。
function test() {
return {
async then(callback) {
const item = await '3';
return callback(item);
}
}
}
test()
.then(console.log)
.then(() => console.log('done'));