【发布时间】:2020-07-17 16:49:23
【问题描述】:
我正在使用 NodeJs v10.19.0 我已经阅读了大量 async/await 文档和教程,但仍然无法正确理解。
我不断向 API 发出获取请求,该请求为我提供了一些 JSON 数据,我希望这些数据在变量中随时可用
我该怎么做?
到目前为止,我已经尝试过以下示例,但没有成功
感谢您的考虑
const rp = require('request-promise');
const myURL = '{A_GET_URL_THAT_RETURNS_A_JSON}';
const options = {uri:myURL, json: true};
const data = ( async ()=>{
try{
return await rp(options)
}catch (e) {
console.log('error \n' + e.stack);
}
})();
console.log(data); // this gets me: "Promise { <pending> }" instead of the json data
我尝试了几种语法,但仍然无法使其工作,也没有意识到我的缺陷在哪里
为什么我不能得到这个的解析值?
【问题讨论】:
标签: javascript node.js promise async-await