I am writing code for getting data.
First I call **getsomedata** function to get data and inside getsomedata function I am calling another function getRandomdata to get data and returning it back to the previous function but it is returning undefined. But in getRandomdata data could be seen in console.log.
Do I need to use callbacks ?
router.get('/get-data', function (req, res, next) {
var result = getsomedata(some_parameter);
console.log(result); // receiving undefined
res.send(result);
});
function getsomedata(some_parameter_recieved) {
var getsomedata = getRandomdata(random_params);
console.log(getsomedata); // receiving undefined
return getsomedata;
}
function getRandomdata(random_params_recieved) {
// after some calculation
console.log(random_data); // receiving proper data
return random_data;
}
Instead of return, you should use callbacks because in asynchronous operations, return does not wait for the I/O operation to complete.
Callback - In JavaScript, higher order functions could be passed as parameters in functions. Since JavaSCript is a single threaded, only one operations happens at a time, each operation thats going to happen is queued in single thread. This way, passed functions(as parameter) could be executed when rest of the parent functions operation(async) is completed and script can continue executing while waiting for results.
Usually this callback function is passed in as the last argument in the function.
Using Callbacks:
router.get('/get-data', function(req, res, next) {
getsomedata(some_parameter, function(result) {
console.log(result);
res.send(result);
});
});
function getsomedata(some_parameter_recieved, callback) {
getRandomdata(random_params, function(random_data) {
callback(random_data);
});
}
function getRandomdata(random_params_recieved, callback) {
// after some calculation
callback(random_data);
}
Using Promise:
router.get('/get-data', function(req, res, next) {
getsomedata(some_parameter, function(result) {
console.log(result);
res.send(result);
});
});
function getsomedata(some_parameter_received, callback) {
getRandomdata(random_params).then(function(random_data) {
callback(random_data);
}).catch(function(e) {
//handle error here
});
}
function getRandomdata(random_params_received, callback) {
return new Promise(function(resolve, reject) {
// after some calculation
if (RandomDataGeneratedSuccessfully) {
resolve(random_data);
} else {
reject(reason);
}
});
}
Related
I'm just starting to work with Javascript and Node, and Async and callbacks concepts are not something I have under control right now.
I have to call a function for each element of a documents Array. This function will call to DB and get me an array of the document annotations. I want to get all the annotations and put them on the same array. Something similar to this:
//function in an async waterfall
function(docs,callback){
let annotationsArray = [];
async.each(docs, (doc, callback2) => {
getAnnotationsFromDocument(doc.Id, callback2);
}, function (err,annotations){
if (err){
callback(err);
}
annotationsArray = annotationsArray.concat(annotations);
callback(null, annotationsArray);
});
},
//Next waterfall function
About the getAnnotationsFromDocument function, this is a simplified structure of it:
function getAnnotationsFromDocument(docId,callback){
initDB();
var async = require('async');
async.waterfall([
function authorize(callback){
//checkAuthorization
(...)
},
function getRfpdocAnnotations(auth, metadata, callback){
//call to DB
(...)
},
function processRfpdocAnnotations(rfpDocAnnotations,metadata,callback){
(...)
callback(null,annotationsList);
}
], function (err, result) {
if(err) {
callback(err);
} else {
callback(null, result);
}
});
}
Unfortunately, I'm unable to code it properly. I'm unable to get the results from the function before exiting the async.each. Could somebody explain me how to structurate the code for this?
Debugging I've found that the function getAnnotationsFromDocument gets the data and execute the last callback(null, result); properly, but when I get to function (err,annotations){, annotations is undefined.
Ok, I think I got it:
First problem was that async.each doesn't return the results on the callback like I was expecting. Unlike waterfall, it just returns the errors. I should have payed more attention reading the documentation.
Secondly, I had to create a callback on the getAnnotationsFromDocument call to process the results.
And finally, I was not executing the call to the callback of async.each, so the execution didn't get to the async.each callback and didn't continue to the next async.waterfall function.
To be quite honest, I'm not sure it's a correct answer, but it does what I was trying to achieve.
// function part of an async.waterfall
function(docs,callback){
let annotationsArray = [];
async.each(docs, (doc,callback2) => {
getAnnotationsFromDocument(doc._id, function(err,result){
if (err){
callback2(err);
}else{
annotationsArray = annotationsArray.concat(result);
}
callback2();
})
}, (err) =>{
if( err ) {
callback(err);
} else {
callback(null,annotationsArray); //to the next waterfall function
}
});
I have a async.each that iterates an array and for every element inside the array it execute a function "check" that has a request inside. This is the code but when I run it I see from the debugger that node doesn't execute the check function and it block its execution.
async.each(array,
function(v_page, callback){
if(v_page.url.length>=5){
internals.check(v_page.url,array2, function (body) {
callback();
});
}
},
function(err){
internals.calls(var1,var2,var3);
});
I tried with a normal for loop but it jump at the internals.calls function without execute the internals.check function for the async nature of node. So how can i force the execution of the check function?
This is the code of the check function:
internals.check = (url,array2,cb) => {
request(url, function(err, recordset) {
if(err){
cb(array2);
}else {
//some stuffs
cb(array2)
}
});
};
You call callback only when v_page.url.length >= 5, but you need to do that for each element:
async.each(array, function(v_page, callback) {
if(v_page.url.length >= 5) {
internals.check(v_page.url,array2, function(body) {
callback();
});
} else {
callback(); <== call callback here, when condition is not met
}
...
Another problem is that, you incorrectly call callback in internals.check. According Node.js notation, the first parameter of callback must be an error or null (async uses this notation). But in your case you call callback with array2 anyway:
internals.check = (url, array2, cb) => {
request(url, function(err, recordset) {
if (err) {
cb(err); // <== pass error here
} else {
cb(null, array2); // <== no error, so first parameter is null, the second is data
}
});
};
I found this example in the docs. I understand how the main callback (function (err,results)...) works but what is the 'callback' parameter that is passed into one and two functions? Where are those coming from and what do they do?
async.parallel({
one: function(callback){
setTimeout(function(){
callback(null, 1);
}, 200);
},
two: function(callback){
setTimeout(function(){
callback(null, 2);
}, 100);
}
},
function(err, results) {
// results is now equals to: {one: 1, two: 2}
});
The callback argument is passed to your functions by the async infrastructure. It points to a function inside of async (though that's not something you need to know). The async infrastructure passes it to you and asks you to call it at the right time.
This is how you communicate back to the async library to tell it that your async function has finished its work and also whether it finished successfully or with an error. When your function finishes it's work, you call that function and that notifies the async library that this step of the process is now done (with either error or success).
FYI, if you're familiar with Express middleware in node.js, it's very similar to the next argument that is passed to middleware. When your middleware gets called, one of the arguments passed to it is the next callback. Your middleware does its work (which could be async) and then when it's done, it tells the Express infrastructure that it's done by calling the next() callback that was passed to it.
Here's a somewhat similar example from Express:
app.use(function (req, res, next) {
doSomethingAsync(function() {
next();
})
});
It doesn't come from anywhere, its just how the library works, a callback is used to pass data to the next function.
This is the function here:
function _parallel(eachfn, tasks, callback) {
callback = callback || noop;
var results = isArrayLike(tasks) ? [] : {};
eachfn(tasks, function (task, key, callback) {
task(rest(function (err, args) {
if (args.length <= 1) {
args = args[0];
}
results[key] = args;
callback(err);
}));
}, function (err) {
callback(err, results);
});
}
https://github.com/caolan/async/blob/master/dist/async.js#L3471
GrabRedisDataByArray = Promise.promisify(function(data, callback) {
var temp_results = [];
async.each(data, function(result, done) {
redis.hgetall('username:' + result, function(err, results) {
temp_results.push(results);
done();
})
}, function(err) {
callback(temp_results)
});
});
Except, it's returning
Unhandled rejection (<[{"server":"9300","user_id":"31","char...>, no stack trace )
--That object data is just some user information stored in the username: key.
And my method for data retrieval is:
GrabRedisDataByArray(data).then(function(data){
console.log(data)
});
I think my problem is with GrabRedisDataByArray and how it doesn't return anything in the main function scope, but only returns something through its second parameter? If that makes sense? I'm just kind of lost here and trying to understand how I can return that async operation to the main function instead of only returning it through the second parameter callback (Which is I think my promises isn't working correctly)
Edit: This is using the Bluebird library
Well, promisify expects node-style callbacks, so when you're calling callback(temp_results) you are passing an error parameter. It would have to be callback(null, temp_results).
That said, you should not use the async library at all when you're working with promises anyway. Just embrace promises. Or if you insist on using async, at least do it properly:
var grabRedisDataByArray = Promise.promisify(function(data, callback) {
async.map(data, function(result, done) {
redis.hgetall('username:' + result, done);
}, callback);
});
But when promisifying, you should always promisify on the lowest level - in your case redis. Then you don't have to mess around with async callbacks but can simply use Bluebird's Promise.map:
Promise.promisifyAll(redis);
function grabRedisDataByArray(data) {
return Promise.map(data, function(result) {
return redis.asyncHgetall('username:' + result);
});
}
So basically I am making a database query, to get all posts with a certain id, then add them to a list, so I can return. But the list is returned, before the callback has finished.
How do I prevent it from being returned before callback has finished?
exports.getBlogEntries = function(opid) {
var list12 =[];
Entry.find({'opid' : opid}, function(err, entries) {
if(!err) {
console.log("adding");
entries.forEach( function(currentEntry){
list12.push(currentEntry);
});
}
else {
console.log("EEEERROOR");
}
//else {console.log("err");}
});
console.log(list12);
return list12;
};
ALL callback is asynchronous, so we don't have any guarantee if they will run exactly in the order we have leave them.
To fix it and make the process "synchronous" and guarantee an order executation you have two solutions:
First: make all process in nested list:
instead of this:
MyModel1.find({}, function(err, docsModel1) {
callback(err, docsModel1);
});
MyModel2.find({}, function(err, docsModel2) {
callback(err, docsModel2);
});
use this:
MyModel1.find({}, function(err, docsModel1) {
MyModel2.find({}, function(err, docsModel2) {
callback(err, docsModel1, docsModel2);
});
});
The last snippet above guarantee us that MyModel2 will be executed AFTER MyModel1 is executed.
Second: Use some framework as Async. This framework is awesome and have several helper functions to execute code in series, parallels, whatever way we want.
Example:
async.series(
{
function1 : function(callback) {
//your first code here
//...
callback(null, 'some result here');
},
function2 : function(callback) {
//your second code here (called only after the first one)
callback(null, 'another result here');
}
},
function(err, results) {
//capture the results from function1 and function2
//if function1 raise some error, function2 will not be called.
results.function1; // 'some result here'
results.function2; // 'another result here'
//do something else...
}
);
You could use sync database calls but that would work around the concept of node.js.
The proper way is to pass a callback to the function that queries the database and then call the provided callback inside the database query callback.
How do I prevent it from being returned before callback has finished?
The callback is asynchronous, and you cannot avoid that. Hence, you must not return a list.
Instead, offer a callback for when it's filled. Or return a Promise for the list. Example:
exports.getBlogEntries = function(opid, callback) {
Entry.find({'opid': opid}, callback); // yes, that's it.
// Everything else was boilerplate code
};
There is an alternate way to handle this scenario. You can use the async module and when the forEach has finished then make the return call. Please find the code snippet below for the same:
var async = requires('async');
exports.getBlogEntries = function(opid) {
var list12 =[];
Entry.find({'opid' : opid}, function(err, entries) {
if(!err) {
console.log("adding");
async.forEachSeries(entries,function(entry,returnFunction){
list12.push(entry);
},function(){
console.log(list12);
return list12;
});
}
else{
console.log("EEEERROOR");
}
});
};