什么是一个简单的实现async.series的? [英] What is a SIMPLE implementation of async.series?

查看:207
本文介绍了什么是一个简单的实现async.series的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个功能:

function one(next){
  Users.find({}, function(err, docs){
    if( err)
      next( err );
    } else {
      next( null );
    }
  });
}

function two(next){
  Something.find({}, function(err, docs){
    if( err)
      next( err );
    } else {
      next( null );
    }
  });
}

我可以使用异步库:

I can use the async library:

async.series( [one, two], function( err ){
  ...
});

下面,如果一个()返回犯错回调马上打电话(含ERR集)。
什么是的容易的,基本实现async.series的?
我看了看code的库异步(这是梦幻般的),但它是一个,就是做了很多的东西,图书馆和我有真正的下面就麻烦了。

Here, the callback is called immediately (with err set) if one() returns err. What's an easy, BASIC implementation of async.series? I looked at the code for the library async (which is fantastic), but it's a library that is meant to do a lot of stuff, and I am having real trouble following it.

你能告诉我一个简单的简单实现async.series的?东西,它可以简单的叫对方后的功能之一,而且 - 如果他们中的一个有错误调用回调 - 是调用与 ERR 设置最终回调?

Can you tell me a simple simple implementation of async.series? Something that it will simply call the functions one after the other, and -- if one of them calls the callback with an error -- is calls the final callback with err set?

谢谢...

芝加哥商业交易所。

推荐答案

一个实现将是这样的:

 function async_series ( fn_list, final_callback ) {
     // Do we have any more async functions to execute?
     if (fn_list.length) {
         // Get the function we want to execute:
         var fn = fn_list.shift();
         // Build a nested callback to process remaining functions:
         var callback = function () {
             async_series(fn_list,final_callback);
         };
         // Call the function
         fn(callback);
     }
     else {
         // Nothing left to process? Then call the final callback:
         final_callback();
     }
 }

以上code不处理结果或错误的处理。要处理,我们可以简单的在回调中检查错误条件错误,立即拨打最终回调的错误:

The above code doesn't handle the processing of results or errors. To process errors we can simply check for error condition in the callback and call the final callback immediately on error:

 function async_series ( fn_list, final_callback ) {
     if (fn_list.length) {
         var fn = fn_list.shift();
         var callback = function (err) {
             if (err) {
                 final_callback(err); // error, abort
             }
             else {
                 async_series(fn_list,final_callback);
             }
         };
         fn(callback);
     }
     else {
         final_callback(null); // no errors
     }
 }

处理结果可以由结果数组的任何跟踪做同样在关闭或通过它向async_series下一个电话。

Handling results can be done similarly by either keeping track of a result array in a closure or passing it on to the next call to async_series.

请注意,最终的最终回调可以假设没有任何错误,因为它通过如果(ERR)到达那里(否则)async_series 电话。

Note that the final final callback can assume no errors because it arrived there via the if(err)(else)async_series call.

这篇关于什么是一个简单的实现async.series的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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