Last active
December 12, 2015 01:18
-
-
Save rhyzx/4689535 to your computer and use it in GitHub Desktop.
next api for async
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// **Compares to waterfall** | |
// waterfall usage | |
async.waterfall([ | |
function(callback){ | |
callback(null, 'one', 'two'); | |
}, | |
function(arg1, arg2, callback){ | |
callback(null, 'three'); | |
}, | |
function(arg1, callback){ | |
// arg1 now equals 'three' | |
callback(null, 'done'); | |
} | |
], function (err, result) { | |
// result now equals 'done' | |
}); | |
// next usage | |
async.next(function (arg1, arg2, callback) { | |
// arg1: 'one', arg2: 'two' | |
callback('three'); | |
}).next(function (arg1, callback) { | |
// arg1: 'three' | |
callback('done'); | |
}).next(function (arg1) { | |
// arg1: 'done' | |
})('one', 'two'); //start | |
// **Resuable** | |
var process = async.next(function (data, next) { | |
// step1, do something | |
next(data); | |
}).next(function (data, next) { | |
// step2, do something... | |
next(data); | |
}); | |
var data1 = new Data(); | |
process(data1); | |
var data2 = new Data(); | |
process(data2); | |
// **Also appendable** | |
process.next(function (data, next) { | |
// step3, do something | |
next(data) | |
}); | |
var data3 = new Data(); | |
process(data3); | |
// **'this' context** | |
var obj = { | |
value : 1, | |
run : async.next(function (v, next) { | |
console.log(this.value); // 1 | |
this.value = v; | |
next(); | |
}).next(function (data, next) { | |
console.log(this.value); // 2 | |
}) | |
}; | |
obj.run(2); | |
// source code here | |
// https://github.com/rhyzx/Lab/blob/master/next-async.js |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment