Answer the question
In order to leave comments, you need to log in
How to make an EventEmitter for multiple simultaneous requests in NodeJS?
I'm requesting two APIs that return source and data store data. I fill the database of my application with them, comparing the source and stored data, for subsequent updating:
var dataStoredLoad = function( dataListOffset ) {
eventEmitter.emit('dataStoredFetched');
}
var dataSourceLoad = function( dataListOffset ) {
eventEmitter.emit('dataSourceFetched');
}
var middlewareDataToUpdate = function ( dataStoredLoad , dataSourceLoad ) {
dataStoredLoad.forEach( function (dataStored, index) {
dataSourceLoad.forEach( function (dataStored, index) {
if ( dataStored.id == dataSource ) {
// key.value comparsion procedure and .pop() / .push()
}
}
}
}
async.series({
one: function(callback) {
var answerAPIone = crunchbase.request(arg1);
callback(null, answerAPIone );
},
two: function(callback) {
var answerAPItwo = airtable.request(arg1);
callback(null, answerAPItwo);
}
},
function(err, results) {
console.log( 'ИТОГО: <', results.one, '><', results.two, '>' );
// ИТОГО: <><>
});
Answer the question
In order to leave comments, you need to log in
https://github.com/caolan/async
async.series({
one: function(callback) {
setTimeout(function() {
callback(null, 1);
}, 200);
},
two: function(callback) {
setTimeout(function() {
callback(null, 2);
}, 100);
}
},
function(err, results) {
// results is now equal to: { one: 1, two: 2 }
});
I advise you to look towards promises.
Start with the canonical promise
library.
Once you get the hang of it, you may need a more complex combine, look towards Q.
// npm install promise
var fs = require('fs');
var Promise = require('promise');
// В качестве источника данных в данном случае файловая система,
// в вашем случае это видимо будет net.Connection
function dataStoredLoad( dataName ) {
return new Promise(function(resolve, reject) {
fs.readFile(dataName, 'utf8', function(err, data) {
if (err) return reject(err);
resolve(data);
});
})
}
// Это другой способ записать то, что написано в dataStoredLoad
function dataSourceLoad( dataName ) {
return Promise.denodeify(fs.readFile)(dataName, 'utf8');
}
Promise.all(dataStoredLoad, dataSourceLoad).then(function(res) {
var stored = res[0],
sourse = res[1];
stored.forEach( function (dataStored, index) {
sourse.forEach( function (dataStored, index) {
if ( dataStored.id == dataSource ) {
// key.value comparsion procedure and .pop() / .push()
}
}
}
});
Once, for a similar problem, a colleague suggested counting events.
also, you can use Promise.all(db_promise, src_promise), with some effort and Promise.race will even be able to handle the timeout.
First, in the case of async, parallel is more suitable for you than series. async.parallel does not wait for the callback from the first function to be called and immediately starts the second one after the first one returns.
Secondly, the APIs you request must be (and most certainly are) asynchronous, and therefore immediately return nothing to you. You must pass a callback to this API, inside which to call the asinka callback.
Something like this:
async.parallel({
one: function(callback) {
crunchbase.request(arg1, function(error, answerAPIone){
callback(null, answerAPIone);
});
},
two: function(callback) {
airtable.request(arg1, function(error, answerAPItwo){
callback(null, answerAPItwo);
});
}
},
function(err, results) {
console.log( 'ИТОГО: <', results.one, '><', results.two, '>' );
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question