【发布时间】:2021-06-03 23:55:09
【问题描述】:
所以我想要做的是继续尝试获取数据检查我想要的值是否存在,如果它不存在,它将返回一个错误,然后重新执行操作,直到该值确实存在。 所以到目前为止我所拥有的是 https 调用
test = async() => {
console.log("Hellow")
now = new Date();
const https = require("https");
https.get("website",{ agent: proxyy },
(res) => {
var body = "";
res.on("data", function (chunk) {
body += chunk;
});
res.on("end", function () {
var resp = JSON.parse(body);
data_wanted = resp.data
const desiredItem = data_wanted.find((item) =>
item.name.includes("data")
);
console.log(desiredItem)
});
}
);
};
我做了什么,但没有成功
while (flag = true ){
let flag = false
try{
await test()// test is the same function above
}catch(e){
flag = true
}
}
因此 try catch 不会捕获任何错误,因此标志不会更改为 true ,并且由于某种原因,while 循环都不会单独工作
EDIT1 所以我认为由于 https 是异步的,try/catch 在 https 调用完成之前正在运行。
【问题讨论】:
-
在完全工作之前我做过的有趣的事情,但我忘记了如何:p
标签: javascript node.js while-loop async-await try-catch