在循环中生成异步javascript任务

在循环中生成异步javascript任务,javascript,asynchronous,promise,Javascript,Asynchronous,Promise,现在我想让示例.getdata异步,但我不想执行if语句,该语句位于for loop之后,直到所有同步任务完成。 这更像是我希望所有的example.getdata函数调用并行执行,在它们完成工作后,我执行if(obj.length) 我尝试使用承诺并推送所有承诺并解决它们,但我不知道如何处理每个函数调用的返回值。您可以使用承诺s 想象一下: function(obj){ for (property in obj) { if (obj.hasOwnProperty(property)) {

现在我想让示例.getdata异步,但我不想执行if语句,该语句位于for loop之后,直到所有同步任务完成。 这更像是我希望所有的example.getdata函数调用并行执行,在它们完成工作后,我执行if(obj.length)

我尝试使用承诺并推送所有承诺并解决它们,但我不知道如何处理每个函数调用的返回值。

您可以使用承诺s

想象一下:

function(obj){
for (property in obj) {
 if (obj.hasOwnProperty(property)) {
    // some code here
   if(condition){
    obj.children = example.getdata(base, obj.name);
     }
  // some more code releated to obj
   }
 }
if (obj.length == 10)
 {
  //some more function
 }
}

其中
get\u jobs
返回一个列表或
Promise
s和
when\u all
将在其所有输入作业都已解析时解析。

要在循环中连续运行异步任务,您不能使用常规的
for
循环,因为
for
循环在执行下一个循环之前不会等待异步操作完成。相反,您必须以稍微不同的方式进行自己的自定义迭代。这里有一个方法:

假设您的
getdata()
函数实际上是异步的,并且有一个完成函数,您可以这样构造:

var jobs = get_jobs(data);
when_all(jobs).done(function (jobs_result) {
    console.log(jobs_result);
});

下面是一个使用承诺的for循环的清晰示例。如果你能选择一个承诺,像Bluebird这样的图书馆会让事情变得更简单:

function myFunc(obj) {
    var keys = Object.keys(obj);
    keys.reduce(function(p, item) {
        return p.then(function(result) {
            // do something with the result of each async operation here
            // and put it in the results array

            return example.getdata(obj[item]);

        });
    }, Promise.resolve());
}

myFunc.then(function(results) {
    // all async operations done here, use the results argument
});

你能用承诺来展示你的设置吗?这是一个非常常见的问题,很容易用承诺来解决。
example.getdata
到底做什么?AJAX请求?是的,getdata正在进行rest调用,但它也在进行许多其他工作,因此使整个函数异步是我的任务goal@Mathletics它遵循var promiseslist promiseslist.push(example.getdata(base,obj.name))的思路;然后是底部的Promise.all(promiseslist)。然后是(function(){});但是我不知道obj.children是如何为每个调用获取其值的。请出示完整的代码好吗?
example.getData
应该只执行一次(或者多次迭代时该条件为真)?为什么你总是覆盖obj.children?它如何在循环中工作,你能给我一个更详细的例子吗?寻找使用承诺的例子,这是一个非常常见的用例。
function myFunc(obj) {
    var keys = Object.keys(obj);
    keys.reduce(function(p, item) {
        return p.then(function(result) {
            // do something with the result of each async operation here
            // and put it in the results array

            return example.getdata(obj[item]);

        });
    }, Promise.resolve());
}

myFunc.then(function(results) {
    // all async operations done here, use the results argument
});
function(obj){

  var props = Object.keys(obj), p = Promise.resolve();
  props.forEach(function(prop){
    p = p.then(function(){
      if(condition){ // can access obj[prop] and obj here
        obj.children = example.getData(...); // getData returns a promise
        return obj.children; // return the promise.
      }
    });
});

Promise.resolve(obj.children).then(function(children){
    // here the async call to get `.children` is done.
    // can wait for `p` too (the loop) if we care about it
    if(obj.length === 10) // do stuff
});