小编Wal*_*ley的帖子

Javascript节点获取同步获取

我正在尝试将node-fetch与nodejs结合使用以对我的个人api进行api调用。我希望能够在此期间定期同步更新某些值,因为在后台我的数据库会进行更新/更改。我知道异步和等待的存在,但我所有的谷歌搜索,我仍然不太了解它们或它们如何与获取请求交互。

这是我尝试开始工作的一些示例代码,但仍然只是未定义的日志

const fetch = require('node-fetch');
const url = 'http://example.com';
let logs;

example();
console.log(logs);
async function example(){
    //Do things here
    logs = await retrieveLogs();
    //Do more things here
}

async function retrieveLogs(){
    await fetch(url)
    .then(res => res.json())
    .then(json => {return json})
    .catch(e => console.log(e))
}
Run Code Online (Sandbox Code Playgroud)

javascript synchronous async-await node-fetch

6
推荐指数
2
解决办法
5764
查看次数

标签 统计

async-await ×1

javascript ×1

node-fetch ×1

synchronous ×1