I am trying to enforce a few asynchronous functions in node.js, to execute synchronously. I am using async.series for that. Here's my code:
async.series([
function(callback){
fs.truncateSync('rooms.txt', 0);
console.log("function1 done");
callback(null, 'one');
},
function(callback){
fs.truncateSync('sids.txt', 0);
console.log("start loop");
var count = 0;
async.whilst(
function () { return count <5 },
function (callback) {
count++;
sid.generateSIDs; //---> PROBLEM!!
callback();
},
function (err) {
}
);
var sids = fs.readFileSync('sids.txt').toString().split('\r\n');
console.log("function2 done");
callback(null, 'two');
},
function(callback){
//lots of asynchronous callbacks
callback(null, 'three');
}
],
function(err, results){
// results is now equal to ['one', 'two', 'three']
});
Everything is working as expected (sequentially) in the above code. But when I am adding the line, sid.generateSIDs;
, marked as 'PROBLEM', everything is going for a toss! generateSIDs
is a function inside an external node.js file called sid.js
. It uses restify to post a couple of HTTP GETs asynchronously. Here is the code inside sid.js
:
'use strict';
var requestify = require('requestify');
var fs = require('fs');
var response = "";
var sessionId="";
function generateSIDs() {
//Request 1: Send Protocol Upgrade Request
response = "";
sessionId="";
requestify.request('http://url.com', {
method: 'GET',
headers: {
'Host': 'given',
'Connection': 'keep-alive',
'Origin': 'given',
'Referer': 'given'
},
cookies: {
'Cookie': 'given'
},
dataType: 'json'
})
.then(function(response) {
// get the response body
response = response.getBody();
sessionId = "extracted from response";
//Request 2: Set up the session
requestify.request('url.com'+sessionId, {
method: 'GET',
headers: {
'Host': 'given',
'Connection': 'keep-alive',
'Origin': 'given',
'Referer': 'given'
},
cookies: {
'Cookie': 'given'
},
dataType: 'json'
})
.then(function(response) {
fs.appendFile('sids.txt', sessionId+'\r\n', function (err) {
if (err) return console.tag("SERVER").log("file not opening: "+err);
});
console.log("file write done");
});
});
};
exports.generateSIDs = generateSIDs();
From inside the async.series()
, generateSIDs()
is getting called once, generating a new sid, and then it is not iterating further. From the console.log()
I could see that because of the asynchronous nature of the restify
call, as soon as sid.generateSIDs()
is called, before even one iteration of this function is finished, the third function(callback)
in async.series()
is getting triggered.
But, I want sid.generateSIDs()
to run multiple times sequentially as per the while loop count. After these runs are done, then only I want third function(callback) to get executed. As you can understand from generateSIDs()
code, I want multiple unique sid's to be created before the third function is called.
When I am running sid.js separately from command line (not in a loop)using node sid.js
, each time it is running fine and creating a new sid for me.
I am new to node.js, and handling synchronous and asynchronous functions together in the same code, messes up my logic!! Can you please help me here?