使用非同步
該非同步包提供功能非同步程式碼。
使用 auto 函式可以定義兩個或多個函式之間的非同步關係:
var async = require('async');
async.auto({
get_data: function(callback) {
console.log('in get_data');
// async code to get some data
callback(null, 'data', 'converted to array');
},
make_folder: function(callback) {
console.log('in make_folder');
// async code to create a directory to store a file in
// this is run at the same time as getting the data
callback(null, 'folder');
},
write_file: ['get_data', 'make_folder', function(results, callback) {
console.log('in write_file', JSON.stringify(results));
// once there is some data and the directory exists,
// write the data to a file in the directory
callback(null, 'filename');
}],
email_link: ['write_file', function(results, callback) {
console.log('in email_link', JSON.stringify(results));
// once the file is written let's email a link to it...
// results.write_file contains the filename returned by write_file.
callback(null, {'file':results.write_file, 'email':'user@example.com'});
}]
}, function(err, results) {
console.log('err = ', err);
console.log('results = ', results);
});
這個程式碼可以通過以正確的順序呼叫 get_data
,make_folder
,write_file
和 email_link
來同步進行。Async 會為你跟蹤結果,如果發生錯誤(callback
的第一個引數不等於 null
),它將停止執行其他功能。