当函数完成时,Nodejs回调函数。

时间:2022-12-08 19:57:22

Supposing we have a script that will execute a certain task for each row in an array.

假设我们有一个脚本,它将为数组中的每一行执行一个特定的任务。

function execute(err, array){
    loop(array, function(err,object){
         console.log(object)
         //do a certain task when it's finished get into the next object successively.
    });
}

function loop(array,callback){
    array.forEach(function(object){
         callback(null, object);
    });
}

function array(callback){
     callback(null, [1, 2, 3, 4, 5]);
}

setTimeout(function(){
    array(execute); 
}, 6000);

Questions:

问题:

  • How to get into the next loop only after finishing the task?
  • 如何在完成任务后进入下一个循环?
  • Is my function considered asynchronous ?
  • 我的函数被认为是异步的吗?

4 个解决方案

#1


1  

Try this:

试试这个:

function execute(err, array) {
    loop(array, function(err, object, next) {
         console.log(object);
         next(); // this will call recur inside loop function
    }, function() {
         console.log('All done');
    });
}

function loop(array, callback, finish) {
    var copy = array.slice();
    (function recur() {
        var item = copy.shift();
        if (item) {
            callback(null, item, recur);
        } else {
            if (typeof finish == 'function') { 
                finish();
            }
        }
    })();
}

No, you function is not asynchronous but you call it asynchronously using setTimeout.

不,您的函数不是异步的,但是您可以使用setTimeout异步调用它。

#2


2  

You can do something like this to iterate over your array :

你可以这样做来遍历你的数组:

var myarray = [1,2,3,4,5];
next(myarray, 0);

function next(array, idx) {
    if (idx !== array.length) {
        // do something
        console.log(array[idx]);

        // run other functions with callback
        another_fct(array[idx], function() {
            next(array, idx + 1); // then the next loop
        });

        // or run directly the next loop
        next(array, idx + 1);
     } else {
        // the entire array has been proceed
        console.log('an array of '+array.length+' number of elements have been proceed');
    }
}

function another_fct(array_element, callback) {
    // do something with array_element
    console.log('value : '+array_element);
    callback(); // run the next loop after processing
}

This method will perform your array elements synchronously.

此方法将同步执行数组元素。

#3


0  

async provides async.series and other helpers for this purpose.

异步提供异步。系列和其他为此目的的辅助程序。

#4


0  

You can wrap your head around it better if you refactor the code and remove any redundant anonymous functions. The case is, that passing an anonymous function as an argument to another function not yet make the function asynchronous. You read more a more in depth explanation in "Does taking a callback make a function asynchronous?".

如果重构代码并删除任何冗余的匿名函数,您可以更好地理解它。情况是,将匿名函数作为参数传递给另一个函数,还没有使该函数成为异步的。在“接受回调是否使函数成为异步的?”

From the refactored version,

从重构版本,

setTimeout(function() {
    [1, 2, 3, 4, 5].forEach(function(object) {
        console.log(object)
        //do a certain task when it's...
    });
}, 6000);

it can be seen that forEach is called for the array. The forEach method executes provided function once per array element and does it synchronously.

可以看到forEach被调用用于数组。forEach方法对每个数组元素执行一次提供的函数并同步执行。

So, to answer your question:

回答你的问题:

  1. yes, it calls the next item only after finishing previous (but if doing anything asynchronous, see below)
  2. 是的,它只在完成前一项之后才调用下一项(但是如果执行异步操作,请参见下面)
  3. this is not considered asynchronous since it doesn't perform any asynchronous operations (not considering the outer setTimeout)
  4. 这并不被认为是异步的,因为它不执行任何异步操作(不考虑外部的setTimeout)

However, if you choose to start an asynchronous operation in the forEach function, then things change considerably. The result is that all operations are in-progress at the same time. This is potentially a hazardous operation resource-wise. There are libraries such as async for handling this use case gracefully.

但是,如果您选择在forEach函数中启动异步操作,那么事情就会发生很大的变化。结果是所有操作同时进行。在资源方面,这是一个潜在的危险操作。有一些库(如异步)可以优雅地处理这个用例。

(Btw, good use of Node.js errbacks, where first function argument reserved for passing potential error.)

(顺便说一句,很好地利用节点。js勘误表,其中第一个函数参数预留给传递潜在错误)

#1


1  

Try this:

试试这个:

function execute(err, array) {
    loop(array, function(err, object, next) {
         console.log(object);
         next(); // this will call recur inside loop function
    }, function() {
         console.log('All done');
    });
}

function loop(array, callback, finish) {
    var copy = array.slice();
    (function recur() {
        var item = copy.shift();
        if (item) {
            callback(null, item, recur);
        } else {
            if (typeof finish == 'function') { 
                finish();
            }
        }
    })();
}

No, you function is not asynchronous but you call it asynchronously using setTimeout.

不,您的函数不是异步的,但是您可以使用setTimeout异步调用它。

#2


2  

You can do something like this to iterate over your array :

你可以这样做来遍历你的数组:

var myarray = [1,2,3,4,5];
next(myarray, 0);

function next(array, idx) {
    if (idx !== array.length) {
        // do something
        console.log(array[idx]);

        // run other functions with callback
        another_fct(array[idx], function() {
            next(array, idx + 1); // then the next loop
        });

        // or run directly the next loop
        next(array, idx + 1);
     } else {
        // the entire array has been proceed
        console.log('an array of '+array.length+' number of elements have been proceed');
    }
}

function another_fct(array_element, callback) {
    // do something with array_element
    console.log('value : '+array_element);
    callback(); // run the next loop after processing
}

This method will perform your array elements synchronously.

此方法将同步执行数组元素。

#3


0  

async provides async.series and other helpers for this purpose.

异步提供异步。系列和其他为此目的的辅助程序。

#4


0  

You can wrap your head around it better if you refactor the code and remove any redundant anonymous functions. The case is, that passing an anonymous function as an argument to another function not yet make the function asynchronous. You read more a more in depth explanation in "Does taking a callback make a function asynchronous?".

如果重构代码并删除任何冗余的匿名函数,您可以更好地理解它。情况是,将匿名函数作为参数传递给另一个函数,还没有使该函数成为异步的。在“接受回调是否使函数成为异步的?”

From the refactored version,

从重构版本,

setTimeout(function() {
    [1, 2, 3, 4, 5].forEach(function(object) {
        console.log(object)
        //do a certain task when it's...
    });
}, 6000);

it can be seen that forEach is called for the array. The forEach method executes provided function once per array element and does it synchronously.

可以看到forEach被调用用于数组。forEach方法对每个数组元素执行一次提供的函数并同步执行。

So, to answer your question:

回答你的问题:

  1. yes, it calls the next item only after finishing previous (but if doing anything asynchronous, see below)
  2. 是的,它只在完成前一项之后才调用下一项(但是如果执行异步操作,请参见下面)
  3. this is not considered asynchronous since it doesn't perform any asynchronous operations (not considering the outer setTimeout)
  4. 这并不被认为是异步的,因为它不执行任何异步操作(不考虑外部的setTimeout)

However, if you choose to start an asynchronous operation in the forEach function, then things change considerably. The result is that all operations are in-progress at the same time. This is potentially a hazardous operation resource-wise. There are libraries such as async for handling this use case gracefully.

但是,如果您选择在forEach函数中启动异步操作,那么事情就会发生很大的变化。结果是所有操作同时进行。在资源方面,这是一个潜在的危险操作。有一些库(如异步)可以优雅地处理这个用例。

(Btw, good use of Node.js errbacks, where first function argument reserved for passing potential error.)

(顺便说一句,很好地利用节点。js勘误表,其中第一个函数参数预留给传递潜在错误)