Iterate over indefinite array of deferred items

后端 未结 1 1737
滥情空心
滥情空心 2020-12-20 08:34

Say I have a very long list where each element requires an asynchronous call to fetch. I would like to write an API on top of that list so that a consumer can simply call \"

相关标签:
1条回答
  • 2020-12-20 09:36

    You can't have such syntax because it would just go into infinite busy loop.

    There is a common promise idiom to do this:

    var array = [process1AndFetch2, ...]
    
    array.reduce(function(a, b) {
        return a.then(process).then(b);
    }, array.shift()()).then(function(){
        //All processed
    });
    

    Assumes jQuery 1.8+

    0 讨论(0)
提交回复
热议问题