【发布时间】:2020-11-23 20:41:21
【问题描述】:
我想在提取完成时获取返回的数据,因为在完全提取后会出现正确的时间戳,但是该函数将返回带有空时间戳的链接,而不是所有数据的最新时间戳。
async function pushData(url) {
let settings = { method: "Get" };
let timestamp = "";
let i = 0;
fetch(url, settings)
.then(res => res.json())
.then((json) => {
json.forEach(function (object) {
console.log(object);
i++;
});
timestamp = json[i-1].timestamp;
});
return await 'https://www.bitmex.com/api/v1/trade?count=1000&symbol=XBTUSD&startTime=' + timestamp;
}
var test = pushData('https://www.bitmex.com/api/v1/trade?count=1000&symbol=XBTUSD');
console.log(test);
【问题讨论】:
-
你为什么不等待获取,而不是一个字符串?
-
尽量不要混用
await和.then -
你的意思是把 await 放在 fetch 前面?这行不通。
-
用法:
const response = await fetch(URL);,参见thisstackoverflow 帖子 -
这能回答你的问题吗? async / await fetch in node-js
标签: javascript node.js