问题描述
我正在使用 node-red flow,在里面我有一个 node 函数,它执行如下操作:
s3.getobject(params,function(err,data) {
if (!err && data && data.Body) {
msg.s3aka = data.Body.toString()
}
});
while (true) {
if (msg.s3aka)
break;
let start = new Date().getTime();
while (true) { // delay 100ms
if ((new Date().getTime() - start) > 100){
break;
}
}
}
return msg;
所以我等待结果 (msg.s3aka) 被设置为返回 msg。这看起来很难看,我不能在节点函数中使用 async/await(不支持 ES 7)。如果我使用 Promise 最后一行(返回 msg)将在 msg.s3aka 设置之前执行。那么有没有更好的方法来使用 vanilla javascript 来做到这一点?
编辑:这不是由 How do I return the response from an asynchronous call? 解决的,因为它特定于节点红色。我尝试了各种方法,包括使用日期、异步等待、承诺。 我通过在回调中使用 node.send(msg) 解决了这个问题。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)