执行以下代码时,只执行async.series的第一个函数。
var fs = require("fs");
var async = require("async");
var buffer = new Buffer(10);
var read = "";
var readByt;
async.series([
function(callback) {
console.log("console: 1");
},
function (callback){
console.log("console: 2");
}
],function(){});
输出如下:
c:\>node fr.js
}}}c:/log.txt
File closed successfully.
如果按以下方式编辑以下内容:
var fs = require("fs");
var async = require("async");
var buffer = new Buffer(10);
var read = "";
var readByt;
async.series([
function(callback) {
console.log("test");
},
function (callback){
console.log("console: 1");
}
function (callback){
console.log("console: 2");
}
],function(){});
输出变化如下:
c:\>node fr.js
test
如何让async.series中的所有函数执行?
答案 0 :(得分:2)
我认为你需要调用函数数组中提供的callback
参数来指示async
模块调用下一个函数。
答案 1 :(得分:1)
var fs = require("fs");
var async = require("async");
var buffer = new Buffer(10);
var read = "";
var readByt;
async.series([
function(callback) {
fs.open('c:/ab.txt', 'r+', function(err, fd) {
fs.read(fd, buffer, 0, buffer.length, 0, function(err, bytes){
read = buffer.slice(0, bytes).toString();
readByt = bytes;
console.log("}}}"+read);
fs.close(fd, function(err){
if (err){
console.log(err);
}
console.log("File closed successfully.");
})
})
})
//invoke here for continue and to break use callback(false); callback()
},
function (callback){
console.log("console:"+read);
console.log("console:"+read.substr(read.length-1));
console.log("console:"+buffer.slice(0, readByt).toString());
}
],function(){});