【发布时间】:2018-12-10 06:25:03
【问题描述】:
我有一个棘手的情况,需要收集属于某些类型(给定数组中的类型)的键,然后过滤收集到的键并传递给删除函数。
收集过程调用外壳代码并在循环内的回调中处理结果。我需要等到整个回调循环完成后再传递给删除函数。
我在节点代码中使用了shelljs,基本上如下所示:
var del_arr = [];
for (var i in types) {
shell.exec(somecode with
var [i], {
silent: true
},
function(code, stdout, stderr) {
somecode-processing/filtering stdout and pushes the results to del_arr;
});
//loop through array types[] and need to wait for all shell codes' callbacks to finish;
}
//then pass del_arr to deletion function
我无法以这种 shelljs 回调的 b/s 格式构建异步函数。我也不知道在这种情况下如何使用promise。
你能告诉我如何实现这个非阻塞进程吗? 谢谢
【问题讨论】:
标签: javascript node.js asynchronous ecmascript-6 es6-promise