Nodejs: Async.series only does the first function and stops

When the following code is executed, only the first async.series function is executed.

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(){});

      

The output is shown below:

c:\>node fr.js
}}}c:/log.txt
File closed successfully.

      

If the following is edited:

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(){});

      

The output changes as follows:

c:\>node fr.js
test

      

How do I get all the functions in async.series to execute?

+3


source to share


2 answers


I think you need to call the argument callback

provided in the array of functions to instruct the module async

to call the next function.



+2


source


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(){});

      

+1


source







All Articles