我如何与承诺这个异步foreach循环的工作? [英] How do I make this async foreach loop work with promises?

查看:152
本文介绍了我如何与承诺这个异步foreach循环的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用它承诺搞砸左右,但我是新来的他们,我只是无法弄清楚如何正确地做到这一点。目前,有没有点到无极,因为它不会等到异步 $。获得完成。

基本上,每个的foreach 迭代都有自己的 $。获得功能,我需要把它们都完成然后继续具有......被albumart部分的console.log

  $。获得(ID,功能(数据){
    //(有一些code这里)
    VAR getZippyUrls =新的承诺(函数(解析){
            zippyarray.forEach(功能(比比){
            //(更多code)
            $获得(zippy.full,功能(数据){
                // ^这是$ .gets的的foreach
               //(code的这里)
            });
           解决(zippyarray);
        });
    });    //这是我失败的承诺 - >
    getZippyUrls.then(功能(响应){
        的console.log(我们OUT+ response.length);
        response.foreach(函数(D){
            的console.log(无极+ d.media);
        });
        的console.log('eyyyyyy');
    });    的console.log(...被albumart);
    //现在previous东西完成后,继续前行


解决方案

在同步code,当行结束进行延续;

通过承诺,继续通过。然后执行。你使用的是承诺的构造函数和解决的立即,你不等待任何任务都没有。我想我的作品映射到任务,然后是链状他们然后或连续等待着他们。

  //我假设
zippyarray; //比比对象数组VAR任务= zippyarray.map(功能(比比,我){
    返回功能(){//返回上比比任务;
       //此处基本逻辑
       返回$获得({
            // ajax请求
       })。然后(功能(数据){
            //处理数据就像在你code
            //后来可能存储供以后使用过
            恢复处理(数据); //返回处理的数据;
       });
    }
});

现在我们可以执行所有这些按顺序:

  VAR P =任务[0](); //启动第一个
 对于(VAR I = 1; I< tasks.length;我++)P = p.then(任务[I]);
 p.then(功能(结果){
       //所有可用这里
 });

或者更好,串行:

  $ when.apply(tasks.forEach(功能(T){返回T();}))。然后(功能(结果){
     // 全做完了
})

I've already messed around with Promises in it, but I'm new to them and I just can't figure out how to do it properly. At the moment, there's no point to the Promise, because it doesn't wait till the async $.get completes.

Basically, each foreach iteration has its own $.get function, and I need to have them all complete and then continue to the part that has the "...gets albumart" console.log.

$.get(id,function(data) {
    //(there's some code here)
    var getZippyUrls = new Promise(function(resolve) {
            zippyarray.forEach(function(zippy) {
            //(more code)
            $.get(zippy.full, function(data) {
                //^This is the foreach of $.gets
               //(code's here)
            });  
           resolve(zippyarray);
        });
    });

    //This is my failed Promise ->
    getZippyUrls.then(function(response) {
        console.log("WE'RE OUT " + response.length);
        response.foreach(function(d) {
            console.log("Promise"+d.media);
        });
        console.log('eyyyyyy');
    });

    console.log("...gets albumart");
    //Now after the previous stuff is done, move on

解决方案

In synchronous code, continuation is performed when the line ends ;

With promises, continuation is performed via .then. You were using a promise constructor and resolved it immediately, you did not wait for any task at all. I'd map my work into tasks and then either chain them with then or await them serially.

//I'm assuming
zippyarray; // array of Zippy objects

var tasks = zippyarray.map(function(zippy,i){
    return function(){ // return a task on that zippy;
       // basic logic here
       return $.get({
            // ajax request
       }).then(function(data){
            // process data like in your code
            // possibly store later for later use too
            return process(data); // return the processed data;
       });
    }
});

Now we can execute them all sequentially:

 var p = tasks[0](); // start the first one
 for(var i = 1; i < tasks.length; i++) p = p.then(tasks[i]);
 p.then(function(result){
       // all available here
 });

Or better, serially:

$.when.apply(tasks.forEach(function(t){ return t(); })).then(function(results){
     // all done
})

这篇关于我如何与承诺这个异步foreach循环的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆