如何返回真正有效的可处理对象



>我正在尝试创建一个返回值但不起作用的可然后对象:

const fetchItem = () => 'item';
function test() {
return {
async then() {
const item = await fetchItem()
return item
}
}
}
test()
.then(console.log)

然后被调用,但控制台.log它不是。有什么想法吗?

.then应该是一个接受回调作为参数的函数 - 您的then定义没有回调。

function test() {
return {
async then(callback) {
const item = await '3';
return callback(item);
}
}
}
test()
.then(console.log)
.then(() => console.log('done'));

最新更新