async.waterfall

async.waterfall

  if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.

async.waterfall([
    function(callback) {
        callback(null, ‘one‘, ‘two‘);
    },
    function(arg1, arg2, callback) {
        // arg1 now equals ‘one‘ and arg2 now equals ‘two‘
        callback(null, ‘three‘);
    },
    function(arg1, callback) {
        // arg1 now equals ‘three‘
        callback(null, ‘done‘);
    }
], function (err, result) {
    // result now equals ‘done‘
});

// Or, with named functions:
async.waterfall([
    myFirstFunction,
    mySecondFunction,
    myLastFunction,
], function (err, result) {
    // result now equals ‘done‘
});
function myFirstFunction(callback) {
    callback(null, ‘one‘, ‘two‘);
}
function mySecondFunction(arg1, arg2, callback) {
    // arg1 now equals ‘one‘ and arg2 now equals ‘two‘
    callback(null, ‘three‘);
}
function myLastFunction(arg1, callback) {
    // arg1 now equals ‘three‘
    callback(null, ‘done‘);
}

参考:https://caolan.github.io/async/docs.html#waterfall

时间: 2025-01-02 15:12:35

async.waterfall的相关文章

async源码学习 - waterfall函数的使用及原理实现

waterfall函数会连续执行数组中的函数,每次通过数组下一个函数的结果.然而,数组任务中的任意一个函数结果传递失败,那么该函数的下一个函数将不会执行,并且主回调函数立马把错误作为参数执行. 以上是我翻译的,我都不知道翻译的什么鬼. 其实该函数的作用就是: 上一个异步函数返回结果可以传给下一个异步函数,如果传递过程中,第一个参数出错了也就是真值的话,下一个回调函数将会停止调用,并且直接调用waterfall函数的第二个参数,其实也是个回调函数.并且把错误的参数传过去. 先看看官网的demo:

使用 async Node.js 简化Javascript代码

async Node.js async 是Javascript的扩展库.它可以简化Node.js异步操作的书写,使代码更容易被读懂,而不是面对多层的括号发疯. 我们可以使用Node.js的包管理器npm直接安装它,在shell中输入: 1 2 npm install async 或者 更改package.json: 1 2 3 4 5 6 7 8 9 10 11 12 13 { "name": "application-name", "version&qu

nodejs异步---Async

一:异步基础 1.由于node.js天生就是异步,所以效率很高,性能强. console.log('hi!'); setTimeout(function(){ console.log('hello!'); },1000); console.log('wow!'); 比如这个,输出结果:hi wow! hello! 可以看出nodejs的异步性 2.高阶函数 高阶函数给我的感觉就是闭包. function test(a){ return function(b){ return a+b; } } v

在NodeJS中使用流程控制工具Async

Async的内容分为三部分: 流程控制:简化十种常见流程的处理 集合处理:如何使用异步操作处理集合中的数据 工具类:几个常用的工具类 本文介绍其中最简单最常用的流程控制部分. 由于nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦.Async的流程控制就是为了简化这些操作. 安装 npm install async --save -d 1.async.waterfall实例(多个函数依次执行,且前一个的输出为后一个的输入) 这个函数名为waterfall(瀑布),可

node.js中async的使用

async,node.js是一个流程控制库. async.series串行执行一组函数,第一个参数是一个由函数组成的数组或json,第二个参数是回调函数.回调函数里先执行task1的返回结果,再处理task2的. task1一旦出错,task2不执行.依次输出task1,task2的result. var async = require("async"); var fs = require("fs"); async.series([ //task1 function

自已实现的async 只实现了一步分功能

不得不说,人和人的技术确实有差距,同样的功能,其他人就是有办写写的更优雅性能更好 不论是C还是js 自已有功能但看着也比人家的丑好多. //最终效果 同async //目前实现了个人最常用的 serial 和 waterfall //实现waterfall // async.waterfall([ // function(callback){ // callback(null, 'one', 'two'); // }, // function(arg1, arg2, callback){ //

使用async解决nodejs异步问题

项目要求:1.对用户的煤.水.电的使用金额对用户进行每周短信提醒. 2.当爬虫爬来的煤.水.电的剩余金额小于10元时,对用户进行短信提醒. 数据库描诉:mongodb  建了4张表    分别分 每周提醒表.水费表.电费表.煤气表 每周提醒表:用户名.用户id.是否开/关提醒功能.用户是否关联煤.水.电.用户电话 电费表:用户名.用户id.社区编号.供电公司编号.是否开/关提醒.剩余电费.用户电话.更新时间.上次更新时间.上次更新剩余电费. 遇到问题描诉:使用nodejs开发,实现每周六晚上短信

珠峰培训node正式课笔记 -- 【async】任务流程控制,异步流程控制

var async = require('async'); // series 串形任务 console.time('cost') async.series({ two:function(callback){ setTimeout(function(){ console.log('串形任务two'); //第一个参数 错误原因,当为真时,接收函数err参数接收到原因,并定为报错,停止执行后边的任务 callback(null,'串形任务two 执行完毕'); },1000) }, one:fun

[Node.js] Promise,Q及Async

原文地址:http://www.moye.me/2014/12/27/promise_q_async/ 引子 在使用Node/JS编程的时候,经常会遇到这样的问题:有一连串的异步方法,需要按顺序执行,前后结果之间有依赖关系,形如(片断1): asyncTask(initial, function (err, result) {//step 1 if (err) throw err; asyncTask(result, function (err, result2) {//step 2 if (e