I need to request data from two web servers. The tasks are independent; therefore, I am using aync.parallel. Now I am only writing 'abc', 'xyz', and 'Done' to the body of my web page.
Since tasks are performed at the same time, can I run into a strange output? E.g.,
xab
cyz
The code.
var async = require('async');
function onRequest(req, res) {
res.writeHead(200, {
"Content-Type" : "text/plain"
});
async.parallel([ function(callback) {
res.write('a');
res.write('b');
res.write('c
');
callback();
}, function(callback) {
res.write('x');
res.write('y');
res.write('z
');
callback();
} ], function done(err, results) {
if (err) {
throw err;
}
res.end("
Done!");
});
}
var server = require('http').createServer(onRequest);
server.listen(9000);
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…