做一个递归的承诺遭受内存泄漏? [英] does a recursive promise suffer memory leak?
本文介绍了做一个递归的承诺遭受内存泄漏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑这个代码片段,函数 b
获得承诺,并在解决问题时调用自身,以获得另一个承诺:
var a = function(){
var timeout = 1000;
让time_promise = new Promise((resolve,reject)=> {
let success = false;
setTimeout(()=> {
document.getElementById('log')。appendChild(document.createTextNode(Called));
resolve();
},timeout);
});
return time_promise;
};
var b = function(){
a()。then(()=> {
//是此次调用后发布的第一个b()
b();
});
};
b();
解决方案
没有递归。 b
不调用自身。回调调用 b
,并在 b
完成后调用回调。
Consider this snippet, where a function b
gets a promise and calls itself when it is resolved in order to get another promise:
var a = function () {
var timeout = 1000;
let time_promise = new Promise((resolve, reject) => {
let success = false;
setTimeout(()=> {
document.getElementById('log').appendChild(document.createTextNode("Called "));
resolve();
}, timeout);
});
return time_promise;
};
var b = function() {
a().then(()=>{
//is the first b() released after this call?
b();
});
};
b();
my question is: is the first call to b released after it called itself? Note the call to itself is inside a function called when then
must be called.
解决方案
There is no recursion. b
doesn't call itself. The callback calls b
and the callback is invoked after b
has finished.
这篇关于做一个递归的承诺遭受内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文