js001 ---- async
程序员文章站
2023-12-13 21:35:10
Node.js异步流,详细见https://caolan.github.io/async/docs.html#parallel 1, async 用的比较多的是 waterfall, 瀑布流, 就是每个异步函数按照顺序执行。如果出错了,可以callback(new Error("xxxx"), Co ......
node.js异步流,详细见https://caolan.github.io/async/docs.html#parallel
1, async
用的比较多的是 waterfall, 瀑布流, 就是每个异步函数按照顺序执行。如果出错了,可以callback(new error("xxxx"), code.fail);
async 最好用的流程控制方法, 可大大降低代码耦合度。 (一个函数只做一件事, async.waterfall则实现了一序列函数的异步组合)
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'); }
parallel async.parallel(tasks, callback)
tasks 并行运行函数集合, 而不必等到上一个函数完成, 如果任何函数发送错误, 会立刻执行回调函数,并返回错误信息; 若没有发生错误, 则会在所有tasks函数执行完毕之后用回调函数将结果返回。
async.parallel([ function(callback) { settimeout(function() { callback(null, 'one'); }, 200); }, function(callback) { settimeout(function() { callback(null, 'two'); }, 100); } ], // optional callback function(err, results) { // the results array will equal ['one','two'] even though // the second function had a shorter timeout. }); // an example using an object instead of an array async.parallel({ one: function(callback) { settimeout(function() { callback(null, 1); }, 200); }, two: function(callback) { settimeout(function() { callback(null, 2); }, 100); } }, function(err, results) { // results is now equals to: {one: 1, two: 2} });
eachseries(coll, iteratee, callbackopt) 跟each一样,不过 一次只运行一个异步操作, the same as but runs only a single async operation at a time.
map 跟each 类似。
// assuming openfiles is an array of file names and savefile is a function // to save the modified contents of that file: async.each(openfiles, savefile, function(err){ // if any of the saves produced an error, err would equal that error }); // assuming openfiles is an array of file names async.each(openfiles, function(file, callback) { // perform operation on file here. console.log('processing file ' + file); if( file.length > 32 ) { console.log('this file name is too long'); callback('file name too long'); } else { // do work to process file here console.log('file processed'); callback(); } }, function(err) { // if any of the file processing produced an error, err would equal that error if( err ) { // one of the iterations produced an error. // all processing will now stop. console.log('a file failed to process'); } else { console.log('all files have been processed successfully'); } });
————————————————————————————————————————————————————————————
以上就是node.js async 的主要知识。