如何异步对象值循环后调用一个函数执行完毕 [英] How to call a function after an asynchronous for loop of Object values finished executing

查看:262
本文介绍了如何异步对象值循环后调用一个函数执行完毕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想经过一个异步循环通过Javascript对象的值迭代完成执行调用函数。我有以下的code

 的(当然在课程){
    VAR URL ='...'+课程[当然]。

    请求(URL(函数(当然){
        返回功能(ERR,RESP,体){
            $ = cheerio.load(体);

            //有些$ C $下,我使用的对象值
        };
    })(课程));
}
 

解决方案

这可以在香草JS做的,但我建议的 异步 模块,它是用来处理异步code在node.js中最流行的库例如, async.each

  VAR异步=需要('异步');

VAR courseIds = Object.keys(课程);

//函数来处理每门课程。
功能perCourse(courseId,回调){
    VAR当然=课程[courseId]

    //做一些事情每门课程。
    回电话();
}

async.each(courseIds,perCourse,功能(错误){
    //执行后,每门课程已经被处理。
});
 

如果您想从每次迭代使用的结果,然后 异步。图 是相似的,但通过结果的数组回调的第二个参数。

如果您preFER香草JS,那么这将工作在 async.each

 函数的每个(单,FUNC,回调){
    //避免清空原来的列表中。
    变种listCopy = list.slice(0);

    //消耗列表中的元素在从左侧一个时间。
    //如果您在使用shift关注的开销
    //可以完成同一个迭代器。
    功能doOne(ERR){
        如果(ERR){
            返回回调(ERR);
        }

        如果(listCopy.length === 0){
            返回回调();
        }

        变种thisElem = listCopy.shift();

        FUNC(thisElem,doOne);
    }

    doOne();
}
 

(取自要点我写了一段时间后)

我强烈建议不过你使用异步库。异步是繁琐写,像功能async.auto 辉煌。

I want to call a function after an asynchronous for loop iterating through values of an Javascript object finishes executing. I have the following code

for (course in courses) {
    var url = '...' + courses[course];

    request(url, (function (course) {
        return function (err, resp, body) {
            $ = cheerio.load(body);

            //Some code for which I use object values    
        };
    })(course));
}

解决方案

This can be done in vanilla JS, but I recommend the async module, which is the most popular library for handling async code in Node.js. For example, with async.each:

var async = require('async');

var courseIds = Object.keys(courses);

// Function for handling each course.
function perCourse(courseId, callback) {
    var course = courses[courseId];

    // do something with each course.
    callback();
}

async.each(courseIds, perCourse, function (err) {
    // Executed after each course has been processed.
});

If you want to use a result from each iteration, then async.map is similar, but passes an array of results to the second argument of the callback.

If you prefer vanilla JS, then this will work in place of async.each:

function each(list, func, callback) {
    // Avoid emptying the original list.
    var listCopy = list.slice(0);

    // Consumes the list an element at a time from the left.
    // If you are concerned with overhead in using the shift
    // you can accomplish the same with an iterator.
    function doOne(err) {
        if (err) {
            return callback(err);
        }

        if (listCopy.length === 0) {
            return callback();
        }

        var thisElem = listCopy.shift();

        func(thisElem, doOne);
    }

    doOne();
}

(taken from a gist I wrote a while back)

I strongly suggest that you use the async library however. Async is fiddly to write, and functions like async.auto are brilliant.

这篇关于如何异步对象值循环后调用一个函数执行完毕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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