async包含了幾個部分,Controlflow(異步流程處理),Utils(工具類),Collections(一些異步數據結構的操做(map objects arrays ))html
流程控制函數:(control flow)git
執行結果保存在series函數額參數 callback(err,res) 的res中,若是其中某個函數調用了callback()而且都一個參數不爲null 那麼將會當即調用最終的callback 中斷未執行的剩餘函數github
async.series([ function(callback) { // do some stuff ... callback(null, 'one'); }, function(callback) { // do some more stuff ... callback(null, 'two'); } ], // optional callback function(err, results) { // results is now equal to ['one', 'two'] }); async.series({ one: function(callback) { setTimeout(function() { callback(null, 1); }, 200); }, two: function(callback){ setTimeout(function() { callback(null, 2); }, 100); } }, function(err, results) { // results is now equal to: {one: 1, two: 2} });
2.parallel(tasks, callback)當即並行執行全部函數數組
最終callback的結果順序就是函數的在數組中的順序,數據結構
若是某個函數出發了錯誤,那麼最終的callback會被當即調用結果包含已經執行完了的函數的結果,剩餘的callback將依然會調用,但最終的calback不會被調用,但結果會傳到最終callback的參數依然有效,如:異步
async.parallel([ function (callback) { // do some stuff ... callback(1111, 'one'); console.log("1111111"); }, function (callback) { // do some more stuff ... callback(null, 'two'); console.log("22222222222222"); } ], // optional callback function (err, results) { // results is now equal to ['one', 'two'] console.log(JSON.stringify(results)); sss = results; }); setTimeout(function(){ console.log(JSON.stringify(sss)); /// 打印最終結果 },1000); /* 輸出: ["one"] 1111111 2222222222 ["one","two"] */
3.waterfall(tasks, callback) (多個函數依次執行,且前一個的輸出爲後一個的輸入)async
若是某個函數觸發了錯誤,那麼下個函數不會被執行,而且會把錯誤傳遞給最終callback,最終callback函數會被馬上調用,函數
/* Runs the tasks array of functions in series, each passing their results to the next in the array. However, 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' });
4. auto(tasks, callback) ( 多個函數有依賴關係, 有的並行執行, 有的依次執行)工具
/* 這裏假設我要寫一個程序,它要完成如下幾件事: 1.從某處取得數據 2.在硬盤上創建一個新的目錄 3.將數據寫入到目錄下某文件 4.發送郵件,將文件以附件形式發送給其它人。 分析該任務,能夠知道1與2能夠並行執行,3須要等1和2完成,4要等3完成。 */ async.auto({ GetData: function (callback) { console.log('.....GetData'); callback(null, "GetData"); }, CreateDir: function (callback) { console.log('.....CreateDir'); callback(null, 'CreateDir'); }, WriteToFile: ["GetData", "CreateDir", function (callback, res) { setTimeout(function () { console.log('.....WriteToFile ' + JSON.stringify(res)); callback(null, 'WriteToFile'); }, 300); }], SendEmail: ["WriteToFile", function (callback, res) { console.log('.....SendEmail ', res); callback(null, "111"); }] }, function (err, res) { console.log('.....done ', res); });
5. whilst(test, fn, callback) 異步的while循環spa
先執行test 若是返回true 那麼調用fn,fn調用錯誤,或者test返回false。那麼馬上調用最終callback
var count = 0; async.whilst( function () { return count < 5; }, function (callback) { count++; setTimeout(function () { console.log(count); callback(null, count); }, 200); }, function (err, n) { console.log(" res " +(count)); } ); console.log("............");
6.queue(worker, number) 把任務添加到一個隊列,經過worker來調用執行任務,parallel執行worker,number容許 worker並行的數量默認是1,也就是依次執行,此外還有一些事件
/* Creates a queue object with the specified concurrency. Tasks added to the queue are processed in parallel (up to the concurrency limit). If all workers are in progress, the task is queued until one becomes available. Once a worker completes a task, that task's callback is called. */ var q = async.queue(function (task, callback) { console.log('start to process task: ' + task.name); callback(); }, 3); // assign a callback q.drain = function () { // 全部任務執行完調用 console.log('all items have been processed'); }; // add some items to the queue q.push({ name: '111111111' }, function (err) { setTimeout(function () { console.log('finished processing 11111111'); }, 1000); }); q.push({ name: '2222222222' }, function (err) { // setTimeout(function () { console.log('finished processing 22222222222'); }, 2000); }); // add some items to the queue (batch-wise) //添加多個任務 q.push([{ name: 'baz' }, { name: 'bay' }, { name: 'bax' }], function (err) { console.log('finished processing item'); }); // add some items to the front of the queue q.unshift({ name: 'bar111' }, function (err) { //添加任務到隊列以前 console.log('finished processing bar111'); }); setTimeout(function () { q.unshift({ name: '3333333333' }, function (err) { console.log('finished processing 444444444444'); }); }, 100); q.saturated = function () { //worker數量用完時調用 console. log('all workers to be used'); } q.empty = function () {//最後一個任務提交worker時調用 console. log('no more tasks wating'); } console.log("............");