【发布时间】:2022-01-18 07:27:37
【问题描述】:
在我的代码中,我有一个返回承诺的异步函数。我知道之前有人问过这个问题,但是没有一个解决方案有效。
const fetch = require('node-fetch');
async function getData() {
const response = await fetch(url);
return (await response.json());
}
getData().then( // wait until data fetched is finished
console.log(getData())
)
提前谢谢你
【问题讨论】:
-
究竟是什么不工作?期望的结果是什么?
-
你还没有问过问题。我猜你想要
getData().then(console.log)或getData().then(data => console.log(data))。 -
@WaisKamal 我正在尝试在函数之外使用响应
-
then函数将 可调用函数 作为参数,请参阅 developer.mozilla.org/de/docs/Web/JavaScript/Reference/… 您的示例与 this 完全相同,但您没有通过一个可调用函数但一个值(console.log()的返回)。
标签: javascript asynchronous promise node-fetch