【发布时间】:2020-07-07 10:28:48
【问题描述】:
我正在尝试在节点中创建一个模块以使用 axios 请求从 API 返回一个 json。但是当我尝试从函数getJson() 中返回响应时,什么都没有返回给我。
const axios = require('axios');
const authentication = require('./authentication');
const url = `https://api.codenation.dev/v1/challenge/dev-ps/generate-data?token=${authentication.token}`;
const getJson = async () => {
const response = await axios.get(url);
// Here i can see the json normally
console.log(response.data)
return response.data
}
const response = getJson()
// but here nothing is shows to me in console.
console.log(response)
【问题讨论】:
-
你的
getJson函数是一个async函数,这意味着它返回一个 Promise 实例。您必须等待另一个带有await的async函数中的承诺,否则添加一个.then()回调。
标签: javascript node.js console return axios