node async function - get the result to the outside function - node.js

I have the following async function and want to have the value data.
I tried with Promises and Async/Awaits. I always miss something. Would be happy for some help. Thanks!
Here the async function:
ad.getData("ID1", function (err, res) {
let data = 11111;
if ((res!= null) && (res.val != null)){
data = res.val;
} else {
// store data
}
});
and I tried:
async function getData(id){
return await ad.getData(id, function (err, res) {
let data = 11111;
if ((res!= null) && (res.val != null)){
data = res.val;
} else {
// store data
}
return data;
})
}
let data = await getData("Id1");
console.log(data);

This is an asynchronous operation, and the code is written in callback style.
ad.getData("ID1", function (err, res) { // the function is a callback
let data = 11111;
if ((res!= null) && (res.val != null)){
data = res.val;
} else {
// store data
}
});
In order to get the result outside the function, or in general, write asynchronous code that look like a synchronous one, we can wrap callback style code in a Promise, like this :
function getDataAsync() {
return new Promise ((resolve, reject) => {
// callback style code is wrapped in the Promise
ad.getData("ID1", function (err, res) {
let data = 11111;
if ((res!= null) && (res.val != null)){
data = res.val;
} else {
// store data
}
resolve(data); // it means, when the Promise is resolved, it will give you the data
});
And you can get the output of getDataAsync like this :
let data = await getDataAsync("Id1");
// we use "await" keyword to wait for the Promise is resolve, data is the variable in the resolve
console.log(data);
You should use Promise instead of callback, and async/await will help you write Promise code easier.
Please take a deep look at https://scotch.io/courses/10-need-to-know-javascript-concepts/callbacks-promises-and-async. When you understand the concept, it's literally easy.

Related

Break from async function in await call

Okay, so I'm working with data from Memcache using a promise based library but the issue I'm having is I don't know a way to break from the async call if a result is found?
The code I'm working with is:
const _pong = function() {
return socket.emit('aye', {
pong: globals.uuid()
});
};
return socket.on('helo', async function(data) {
socket._uuid = data.uuid;
let key = 'ws-ping:' + data.uuid;
await cache.get(key).then((result) => {
if(result !== undefined) {
_pong();
}
});
......
});
I basically need to just ignore the rest of the socket.on function if a result is found using the given key? but it seems to continue?
Because you're using await, you can ditch the .then, and get the result directly, in the same block - if the result exists, then just return (after _ponging, if that's the logic you're looking for):
return socket.on('helo', async function(data) {
socket._uuid = data.uuid;
let key = 'ws-ping:' + data.uuid;
const result = await cache.get(key);
if (result !== undefined) {
_pong();
return;
}
// ...
});

BlueBird Promises returns "Undefined" when return inside "then" function

Here is my Code:
Checking if user is folowing official Twitter Account (here I've returned new Promise
var isFollowing = function(sender) {
return new promise(function(resolve, reject) {
var following = false;
var params = {
source_id: sender,
target_screen_name: config.get('twitter.official_account')
};
TwitObj.get('friendships/show', params, function(err, data) {
if (!err) {
following = data.relationship.source.following;
resolve(following);
} else {
reject(err);
}
});
});
};
Validation:
var validateMsg = function(dm) {
var sender = getSender(dm);
var result = [];
isFollowing(sender.id)
.then(function(response) {
if (!response) {
result = interactiveMessage(false, lang.__('FOLLOW_MAIN', sender.name));
console.log(result); // Displays as expected
return result; // Not returning value Do I need to return promise again? If yes, WHY?
}
});
};
Main Implementation:
var direct_message = function(msg) {
verifyAccount().catch(function(err) {
console.error(err);
});
var dm = msg.direct_message;
var result = validateMsg(dm);
console.log(result);
};
Questions is how should I force validateMsg function to return result variable inside then function.
Update: While debugging, I got to know that, console.log(response) in
validation function is displaying later after throwing undefined in
"then" function which means program is not able to get response
immediately and due to async nature, I/O is not getting blocked. How
to tackle this?
You're not actually returning anything in the validateMsg function. You're returning a synchronous value ( result ) in the .then function which is a separate function.
The second thing to consider is that you're expecting the validateMsg function, which is asynchronous to behave synchronously.
var result = validateMsg(dm);
The way to achieve what I believe you're looking to do involves making the following changes:
1) Return the promise chain in the validateMsg function.
var validateMsg = function(dm) {
var sender = getSender(dm);
var result = [];
return isFollowing(sender.id)
.then(function(response) {
if (!response) {
result = interactiveMessage(false, lang.__('FOLLOW_MAIN', sender.name));
return result;
}
// Not sure what you want to do here if there is a response!
// At the moment you're not doing anything if the validation
// is successful!
return response;
});
};
2) Change your function call, since you're now returning a promise.
validateMsg(dm).then( function( result ){
console.log( result );
})
3) Consider adding a catch somewhere if only to help you debug :)
validateMsg(dm).then( function( result ){
console.log( result );
})
.catch( function( err ){
console.log( "Err:::", err );
});
The validationMsg function does not have a return value, thus the result in the main function has the value undefined. Try to put return in front of isFollowing so the function returns a promise, then treat validationMsg as a promise.

Mongodb iterate a collection of in a synchronous way using promises

I have a method (see code example) and I'm trying to return a promises list to parent method. My problem is when pushing promises in my promiseList array.
getDistance is a method returning a Promise. If I do .then() in getDistance method,everything is OK. Nevertheless, if I try to push the promise into my array, when I do .then in parent method, it is empty.
I think it may happen due to each loop is asynchronous...
var promiseList = [];
res = db.collection.find query statement...
res.each(function(err, doc) {
if (doc!==null) {
promiseList.push(
getDistance(orgLat, orgLon, destLat, desLon)
);
}
});
return Promise.all(promTimeList);
I'm using MongoDB as database, and NodeJS in server side, with mongodb as driver to connecto to my database and Bluebird as library to implement promises.
Thanks in advance!
I think you are correct in that the issue is due to the fact that each is asynchronous. You can use defer to wrap callback APIs (like each) into promises. It would work something like this:
res = db.collection.find query statement...
processResponse(res)
.then(function(promiseList) {
// execute all the promises
return Promise.all(promiseList);
}, function (err) {
// handle error
});
function processResponse(res) {
var deferred = Promise.pending();
var promiseList = [];
res.each(function(err, doc) {
if (err) {
// error, abort
deferred.reject(err);
return;
}
else if (doc != null) {
promiseList.push(
getDistance(orgLat, orgLon, destLat, desLon)
);
}
else {
// finished looping through all results, return the promise list
deferred.resolve(promiseList);
}
});
return deferred.promise;
}
See more on defer with bluebird at the following link (look for "So when should deferred be used?"):
https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns
Update: According to this post:
Define empty Bluebird promise like in Q
It looks like there is a Promise constructor that may be the preferred way to do this. The processResponse method would look like this:
function processResponse(res) {
return new Promise(function(resolve, reject) {
var promiseList = [];
res.each(function(err, doc) {
if (err) {
// error, abort
reject(err);
return;
}
else if (doc != null) {
promiseList.push(
getDistance(orgLat, orgLon, destLat, desLon)
);
}
else {
// finished looping through all results, return the promise list
resolve(promiseList);
}
});
});
}
Thanks to #Bergi. Please correct me if I am wrong. I am more familiar with the Q library (https://github.com/kriskowal/q) than bluebird.
Iterate the collection syncrhonously by using await with cursor.next() in a while loop:
var cursor = db.collection.find({});
while ((doc = await cursor.next()) != null) {
promiseList.push(
getDistance(doc.orgLat, doc.orgLon, doc.destLat, doc.desLon);
);
}

NodeJs Mongoose How can I get out a data from "find().then" in a "find().then"?

Sorry for my Title, I don't know what can I put.
Can you help me please, I would like to print data from a "then" in a "then" ?
Thank you
models.book.find()
.then( function (content) {
var i = 0;
while (content[i]) {
models.author.findOne({"_id": content[i].author_id}, function(err, data) {
console.log(data); //here, it' good
content[i] = data;
MY_DATA = content;
return MY_DATA;
});
i++;
};
})
.then(function (result) {
console.log(result); // here I would like to print MY_DATA
});
There are a number of problems with your code, and I don't think it's behaving as you're expecting it to.
Chaining Promises
In order to effectively chain promises how you're expecting, each promise callback needs to return another promise. Here's an example with yours changed around a bit.
var promise = models.book.find().exec(); // This returns a promise
// Let's hook into the promise returned from
var promise2 = promise.then( function (books) {
// Let's only get the author for the first book for simplicity sake
return models.author.findOne({ "_id": books[0].author_id }).exec();
});
promise2.then( function (author) {
// Do something with the author
});
In your example, you're not returning anything with your callback (return MY_DATA is returning within the models.author.findOne callback, so nothing happens), so it's not behaving as you're expecting it to.
model.author.findOne is asynchronous
model.author.findOne is asynchronous, so you can't expect to call it multiple times in the callback without handling them asynchronously.
// This code will return an empty array
models.book.find( function (err, books) {
var i = 0, results = [];
while (books[i]) {
models.author.findOne({ "_id": books[i].author_id}, function (err, data) {
// This will get called long after results is returned
results.push(data);
});
i++;
};
return results; // Returns an empty array
});
Handling multiple promises
Mongoose uses mpromise, and I don't see a method to handle multiple promises together, but here's a way your case could be done.
var Promise = require('mpromise');
models.book.find().exec()
.then( function (books) {
var i = 0,
count = 0,
promise = new Promise(),
results = [];
while (books[i]) {
models.author.findOne({ "_id": books[i].author_id }, function (err, author) {
results.push(author);
count++;
// Keep doing this until you get to the last one
if (count === books.length) {
// Fulfill the promise to get to the next then
return promise.fulfill(results);
}
return;
});
}
return promise;
})
.then( function (results) {
// Do something with results
});
I don't know if this will work exactly like it is, but it should give you an idea of what needs to be done.

node.js callback function at the after loop has ended

I have an array of URLs and I want to loop through them and fetch thr content. After I have looped through them and fetched thr content I want a callback function to be called.
I know I can do this via async library but I want to do this without using any library.
Sample of what kind of code I want is below
['yahoo.com', 'gmail.com'].each(function(item){
//code to fetch URL content
},someCallbackFunctionToBeExecutedAtTheEndOfLoop);
This is typically the type of thing you do using promises (But you would need a library), with a code like:
var ops = [];
urls.forEach(function(url) {
ops.push(fetchUrl(url));
});
P.all(ops).then(callback);
function fetchUrl(url) {
var defer = P.defer();
//do stuff
// call defer.resolve(result);
return defer.promise;
}
If you don't want to use promises, you can use a counter of operations, like:
var ops = urls.length;
urls.forEach(function(url) {
// do stuff
ops--;
if (ops === 0) {
callback();
}
});
If you chose the promises, I advice to use p-promise module, which is far more optimized than Q.
If you want to do it without any sort of library like async, then you have to write your own counter to keep track of when all the async responses have been completed:
var request = require('request');
function loadAll(list, fn) {
var cnt = list.length;
var responses = [];
list.forEach(function(url, index) {
request(url, function(error, response, body) {
if (error) {
fn(error);
} else {
responses[index] = response;
--cnt;
if (cnt === 0) {
fn(0, responses);
}
}
});
})
}
loadAll(['http://www.yahoo.com', 'http://www.gmail.com'], function(err, results) {
if (!err) {
// process results array here
}
});
If you're going to be doing many async operations in node.js, then getting a promise library like Bluebird will save you a lot of time. For example, I think you could do the above in something like this (untested):
var Promise = require("bluebird");
var requestP = Promise.promisfy(require("request"));
Promise.map(['http://www.yahoo.com', 'http://www.gmail.com'], requestP).then(function(results) {
// process the array of results here
});

Resources