【发布时间】:2015-10-09 20:28:17
【问题描述】:
我需要以“并行”方式执行异步函数,并以最佳结果继续执行程序。因此我写了这样的东西:
var p = [];
for (var i = 0; i < 10; ++i) (function (index) {
p.push(new Promise(function (resolve, reject) {
setTimeout(function () {
var success = Math.random() > 0.7;
console.log("Resolving", index, "as", success ? "success" : "failure");
success && resolve(index);
}, Math.random() * 5000 + 200);
}));
})(i);
Promise.race(p).then(function (res) {
console.log("FOUND", res);
}).catch(function (err) {
console.log("ERROR", err);
});
现在,我想知道在使用 Promise 时这是否是一种好习惯?不是更频繁地解决或拒绝它们然后任何东西都会造成内存泄漏吗?他们最终每次都被 GC 处理了吗?
【问题讨论】:
标签: javascript memory-leaks es6-promise