This question already has answers here:
How do I return the response from an asynchronous call?
(41 answers)
Closed 1 year ago.
I'm trying to get the value returned from an API query, but in all the ways I've done it, it either returns Undefined or [object Promise]. I've tried several solutions and I still haven't got something that works. Below is the last code I made to see if it worked but again without success.
function generateLink(link) {
const url = 'https://api.rebrandly.com/v1/links';
const options = {
method: 'POST',
uri: url,
headers: {'Content-Type': 'application/json', apikey: 'xxxxxxxxxxxxxxxxxxxxx'},
body: JSON.stringify({destination: link})
};
return requestPromise(options).then(response => {
if ( response.statusCode === 200 ) {
return response.body
}
return Promise.reject(response.statusCode)
})
}
...
bot.onText(/\/offers (.+)/, function onEchoText(msg, match) {
console.log(match[1]);
if (isNaN(match[1])) {
bot.sendMessage(msg.from.id, 'Enter a valid number! \nExample: /offers 5');
} else {
client.execute('aliexpress.affiliate.product.query', {
'app_signature': 'defualt',
'category_ids': '701,702,200001081,200001388,200001385,200386159,100000616,100001205,5090301',
'target_currency': 'USD',
'target_language': 'EN',
'tracking_id': 'defualt',
'ship_to_country': 'US',
}, function (error, response) {
var code = response.resp_result.resp_code;
var mesage = response.resp_result.resp_msg;
if (code === 200) {
var i;
var temCupom = [];
var link = [];
var itemList = response.resp_result.result.products.product;
for (i = 0; i < match[1]; i++) {
temCupom[i] = itemList[i].promo_code_info ? "π· <b>There's a coupon!</b>: " + itemList[i].promo_code_info.promo_code : "";
(async () => {
link[i] = generateLink(itemList[i].promotion_link).then(body => { return body.shortUrl })
})();
bot.sendPhoto(msg.chat.id, itemList[i].product_main_image_url, {
caption: "β€ <b>Promotion</b> β€\n" +
"π΅ <b>Price</b>: " + Number(itemList[i].target_sale_price).toLocaleString('en-us', { style: 'currency', currency: 'USD' }) + "\n" +
"π <b>Link</b>: " + link[i] + "\n" +
temCupom[i],
});
}
}
else {
bot.sendMessage(msg.from.id, 'Deu errado! ' + mesage);
}
})
}
});
link[i] need to return the links generated with the API
Option 1 (with async/await):
if (code === 200) {
var i;
var link = [];
var itemList = response.resp_result.result.products.product;
for (i = 0; i < match[1]; i++) {
link[i] = await generateLink(itemList[i].promotion_link).then(body => { return JSON.parse(body).shortUrl })
}
}
Option 2 (with Promise):
if (code === 200) {
var link = [];
var itemList = response.resp_result.result.products.product;
for (let i = 0; i < match[1]; i++) {
generateLink(itemList[i].promotion_link).then(body => { link[i] = JSON.parse(body).shortUrl })
}
}
Option 3 (Promise.all takes all URLs in parallel mode):
if (code === 200) {
const itemList = response.resp_result.result.products.product;
const requests = itemList.slice(0, match[1])
.map(item => generateLink(item.promotion_link).then(body => JSON.parse(body).shortUrl));
const links = await Promise.all(requests);
}
===============================
Updated:
After all, we realized that the body was a string and it was necessary to do JSON.parse
You need to assign the result in the then block.
You can make some simple changes, like putting link[i] = result into the then block, but there are problems like the fact that value of i will be the same. Also, if you want the values populated after the for loop, you'll notice that they have not been filled. You need to wait till they are all resolved (faking with timeout below).
function generateLink(link) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(Math.random())
}, 1)
})
}
if (200 === 200) {
var i;
var link = [];
var MAX = 10;
for (i = 0; i < MAX; i++) {
generateLink('π€·ββοΈ').then(result => {
link[i] = result; // no bueno - since i will be 10 when this is resolved
})
}
console.log(link) // still not resolved
setTimeout(() => {
console.log(link) // π€¦ββοΈ [undefined * 9, value]
}, 10)
}
// async version (note use of `async`)
(async function(){
var MAX = 10;
var link = []
for (i = 0; i < MAX; i++) {
link[i] = await generateLink('π€·ββοΈ')
}
console.log(link)// π
})()
So you can add more complexity to make it work, but async/await is likely the way to go if you can support it.
for (i = 0; i < match[1]; i++) {
link[i] = await generateLink(itemList[i].promotion_link).then((body) => {
return body.shortUrl;
});
}
Also if you use await the promises will execute in series, which you might not want. Resolving in parallel is faster, but you could also run into issue if you're going to have too many network requests they'll need to be throttled, so async await may work better)
Related
I am doing some practice in node.js. In this exercise I been asked to find a country name through a GET Http Request to an endpoint passing a page integer as a parameter.
Where the important response structs are these {page, total_pages, data}.
page is the current page,
total_pages is the last page,
data is an array of 10 country object.
In getCountryName func I am able to retrieve the right answer only if the answer is on the 1st page, the 1 iteration of the loop. So, why the loop only happens once?
Aditional, I wanted to retrieve the total_pages to replace the hardcode '25' value but I do not figure it out how to return it along with the search.
Any hint you wanna give me? The whole problem is in getCountryCode func.
'use strict';
const { Console } = require('console');
const https = require('https');
function makeRequest(page){
return new Promise(resolve => {
let obj='';
https.get('https://jsonmock.hackerrank.com/api/countries?page='+page, res => {
let data ='';
res.on('data',function(chunk){
data+=chunk;
});
res.on('end',function(){
obj=JSON.parse(data);
resolve(obj);
});
});
});
}
async function getCountryName(code) {
var res = '';
var pages = 25;
var i = 1;
while(i <= pages && res == ''){
console.log(i);
res = makeRequest(i)
.then(data => {
let f = ''
let p = data['total_pages'];
let search = data['data'].find(o => o.alpha3Code === code);
f = search != null ? search['name'] : f;
return f;
});
i++;
}
return res;
}
async function main() {
const name = await getCountryName('ARG');
console.log(`${name}\n`);
}
main();
Without modifying your code too much, this is how you do it:
'use strict';
const { Console } = require('console');
const https = require('https');
function makeRequest(page){
return new Promise(resolve => {
let obj='';
https.get('https://jsonmock.hackerrank.com/api/countries?page='+page, res => {
let data ='';
res.on('data',function(chunk){
data+=chunk;
});
res.on('end',function(){
obj=JSON.parse(data);
resolve(obj);
});
});
});
}
async function getCountryName(code) {
const pages = 25;
var i = 1;
let f = null
while(i <= pages && f === null){
console.log(i);
const data = await makeRequest(i) // put in try/catch
const p = data['total_pages'];
const search = data['data'].find(o => o.alpha3Code === code);
f = search !== null ? search['name'] : null;
i++;
}
return res;
}
async function main() {
const name = await getCountryName('ARG');
console.log(`${name}\n`);
}
main();
Hi I have a for loop in my node js application which calls an async function. I want to check a value and decide whether a customer is found or not. But the loop iterates until the last element. Hence my error loop is not working. I want the loop to check the response and then iterate the next loop.
for loop:
for (let i = 0; i < customerlookupresponse.data.length; i++) {
var customer = customerlookupresponse.data[i];
if (customer != undefined) {
console.log("customer.id :: " + customer.id)
var accountlookUpData = {
customerId: customer.id
};
customerAccountLookUpRequest(accountlookUpData).then(data => {
console.log("----" + i + " -- " + data);
if (data && data.status === 1) {
resolve(data);
return;
}else{
reject({
status: 404,
message: "Customer not found"
});
return;
}
});
} else {
reject({
status: 404,
message: "Customer not found"
});
return;
}
}
the async function:
async function customerAccountLookUpRequest(customerLookUpData) {
var accountLookUp = config.app.url;
let data = await axios.get(accountLookUp).then(accountLookUpResult => {
for (i = 0; i < accountLookUpResult.data.length; i++) {
var requestaccount = accountLookUpResult.data[i].accountNumber;
if (requestaccount == customerLookUpData.lookupAccount) {
accountLookUpResult.data[i].customerId = customerLookUpData.customerId;
accountLookUpResult.data[i].status = 1;
return accountLookUpResult.data[i];
}
}
});
return data;
}
I am new to node js and trying to understand the concept of async await. Please help.
An async function waits for a Promise to return. The function that has the loop should be declared as async and the customerAccountLookUpRequest function should return a promise. Then use the await operator to call the function. Simple example:
class some_class {
constructor() {
}
async my_loop() {
let _self = this;
for (let i = 0; i < customerlookupresponse.data.length; i++) {
let data = await _self.customerAccountLookUpRequest(accountlookUpData);
console.log("----" + i + " -- " + data);
}
}
customerAccountLookUpRequest(customerLookUpData) {
return new Promise((resolve, reject) => {
axios.get(accountLookUp).then(accountLookUpResult => {
resolve(accountLookUpResult);
});
});
}
}
The return Promise.all([photoArray]) returns an empty array, seemingly not waiting for the callFB to return its promise that then pushes into the array.
I am not sure what I am doing wrong but am relatively new to Promises with for loops and Ifs.
I am not sure exactly if I am using the correct number of Promises but I seem to not be able to get the 3rd tier Promise.all to wait for the for loop to actually finish (in this scenario, the for loop has to look through many item so this is causing an issue where it is not triggering callFeedback for all the items it should before context.done() gets called.
I have tried using Q.all also for the Promise.all([photoArray]) but have been unable to get that working.
module.exports = function (context, myBlob) {
var res = myBlob
var promiseResolved = checkPhoto(res,context);
var promiseResolved2 = checkVideo(res,context);
Promise.all([promiseResolved, promiseResolved2]).then(function(results){
context.log(results[0], results[1]);
// context.done();
});
});
};
};
function checkPhoto(res, context){
return new Promise((resolve, reject) => {
if (res.photos.length > 0) {
var photoArray = [];
for (var j = 0; j < res.photos.length; j++) {
if (res.photos[j].feedbackId !== null){
var feedbackId = res.photos[j].feedbackId;
var callFB = callFeedback(context, feedbackId);
Promise.all([callFB]).then(function(results){
photoArray.push(results[0]);
});
} else {
photoArray.push("Photo " + j + " has no feedback");
}
}
return Promise.all([photoArray]).then(function(results){
context.log("end results: " + results);
resolve(photoArray);
});
} else {
resolve('No photos');
}
})
}
function checkVideo(res, context){
return new Promise((resolve, reject) => {
same as checkPhoto
})
}
function callFeedback(context, feedbackId) {
return new Promise((resolve, reject) => {
var requestUrl = url.parse( URL );
var requestBody = {
"id": feedbackId
};
// send message to httptrigger to message bot
var body = JSON.stringify( requestBody );
const requestOptions = {
standard
};
var request = https.request(requestOptions, function(res) {
var data ="";
res.on('data', function (chunk) {
data += chunk
// context.log('Data: ' + data)
});
res.on('end', function () {
resolve("callFeedback: " + true);
})
}).on('error', function(error) {
});
request.write(body);
request.end();
})
}
The code suffers from promise construction antipattern. If there's already a promise (Promise.all(...)), there is never a need to create a new one.
Wrong behaviour is caused by that Promise.all(...).then(...) promise isn't chained. Errors aren't handled and photoArray.push(results[0]) causes race conditions because it is evaluated later than Promise.all([photoArray])....
In case things should be processed in parallel:
function checkPhoto(res, context){
if (res.photos.length > 0) {
var photoArray = [];
for (var j = 0; j < res.photos.length; j++) {
if (res.photos[j].feedbackId !== null){
var feedbackId = res.photos[j].feedbackId;
var callFB = callFeedback(context, feedbackId);
// likely no need to wait for callFB result
// and no need for Promise.all
photoArray.push(callFB);
} else {
photoArray.push("Photo " + j + " has no feedback");
}
}
return Promise.all(photoArray); // not [photoArray]
} else {
return 'No photos';
};
}
callFB promises don't depend on each other and thus can safely be resolved concurrently. This allows to process requests faster.
Promise.all serves a good purpose only if it's used to resolve promises in parallel, while the original code tried to resolve the results (results[0]).
In case things should be processed in series the function benefits from async..await:
async function checkPhoto(res, context){
if (res.photos.length > 0) {
var photoArray = [];
for (var j = 0; j < res.photos.length; j++) {
if (res.photos[j].feedbackId !== null){
var feedbackId = res.photos[j].feedbackId;
const callFBResult = await callFeedback(context, feedbackId);
// no need for Promise.all
photoArray.push(callFBResult);
} else {
photoArray.push("Photo " + j + " has no feedback");
}
}
return photoArray; // no need for Promise.all, the array contains results
} else {
return 'No photos';
};
}
Add try..catch to taste.
i having Api Call which execute in For Loop some of the value which returns 10 sec itself some may take nearly 60 sec i have to maintain proper Timeout and clear session (i.e if results comes at 15 sec means it should goes to next input values and run the code) but currenly its waiting for 45 sec each single record how to optimize it
here my sample code :
if (selectedrows.length >= 1) {
for (var i = 0; i < selectedrows.length; i++) {
var myVar = setTimeout (function (k) {
var ob = { results: "Appending ..." };
child.update(selectedrows[k][4], selectedrows[k][4], ob);
var fullName = selectedrows[k][1] + ' ' + selectedrows[k][2];
math.ResultCall.async(fullName,function (err, res) {
if (err) throw err;
var returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null)
{
console.log("None found")
}
else{
var obj = { results: βresβ };
child.update(selectedrows[k][4], selectedrows[k][4], obj);
}
}
});
}, i * 45000,i);
}
}
Rephrasing your question, you need to return the data when your api gets resolved.
For this please go through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve
JavaScript, by default it work asynchronously because of its event loop.
You have promises and resolve to get notified when your api returns a data
Hope I helped :)
There are several approaches to implement the solution
1. Async-Await: in-case the records-processing order is important
for( let i=0; i<selectedrows.length; i++)
{
let ob = { results: "Appending ..." };
child.update(selectedrows[i][4], selectedrows[i][4], ob);
let fullName = selectedrows[i][1] + ' ' + selectedrows[i][2];
await new Promise((resolve,reject)=>
{
math.ResultCall.async(fullName,(err, res) => {
if (err) reject(err);
let returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null) {
console.log("None found")
} else {
let obj = { results: βresβ };
child.update(selectedrows[i][4], selectedrows[i][4], obj);
}
resolve();
});
}
**don't forget this means the wrapping function should be async as well (which returns a promise that can be resolved if necessary)
2.Promise.All: if the order is not important
let promArray = [];
for( let i=0; i<selectedrows.length; i++)
{
let ob = { results: "Appending ..." };
child.update(selectedrows[i][4], selectedrows[i][4], ob);
let fullName = selectedrows[i][1] + ' ' + selectedrows[i][2];
promArray.push( new Promise((resolve,reject)=>
{
math.ResultCall.async(fullName,(err, res) => {
if (err) reject(err);
let returnedValue = JSON.parse(res);
console.log(returnedValue);
if(returnedValue.Result == null || returnedValue.Result.FOUND_Result == null) {
console.log("None found")
} else {
let obj = { results: βresβ };
child.update(selectedrows[i][4], selectedrows[i][4], obj);
}
resolve();
});
);
}
Promise.all(promArray);
** this will also return a Promise that can be resolved if necessary.
This question already has answers here:
Wait until all promises complete even if some rejected
(20 answers)
Closed 5 years ago.
I am trying to use request-promise module to check multiple web sites. If I use Promise.all as per design, promise returns with first reject. What is the proper way to execute multiple request tasks and wait all requests to finish whether they are fulfilled or rejected? I have come up with following two functions.
CheckSitesV1 returns exception due to one rejected promise. However CheckSitesV2 waits all promises to finish whether they are they are fulfilled or rejected. I will appreciate if you can comment whether the code I write makes sense. I am using NodeJS v7.9.0
const sitesArray = ['http://www.example.com','https://doesnt-really-exist.org','http://www.httpbin.org'];
async function CheckSitesV1() {
let ps = [];
for (let i = 0; i < sitesArray.length; i++) {
let ops = {
method: 'GET',
uri:sitesArray[i],
};
const resp = await rp.get(ops);
ps.push(resp);
}
return Promise.all(ps)
}
function CheckSitesV2() {
let ps = [];
for (let i = 0; i < sitesArray.length; i++) {
let ops = {
method: 'GET',
uri:sitesArray[i],
};
ps.push(rp.get(ops));
}
return Promise.all(ps.map(p => p.catch(e => e)))
}
CheckSitesV1().then(function (result) {
console.log(result);
}).catch(function (e) {
console.log('Exception: ' + e);
});
CheckSitesV2().then(function (result) {
console.log(result);
}).catch(function (e) {
console.log('Exception: ' + e);
});
Your
function CheckSitesV2() {
let ps = [];
for (let i = 0; i < sitesArray.length; i++) {
let ops = {
method: 'GET',
uri:sitesArray[i],
};
ps.push(rp.get(ops));
}
return Promise.all(ps.map(p => p.catch(e => e)))
}
is totally fine. I can only suggest refactor a bit for readability:
function CheckSitesV2() {
let ps = sitesArray
.map(site => rp.get({method: 'GET', uri: site}).catch(e => e));
return Promise.all(ps);
}
Regarding async try this
async function CheckSitesV1() {
let results = [];
for (let i = 0; i < sitesArray.length; i++) {
let opts = {
method: "GET",
uri: sitesArray[i]
};
const resp = await rp.get(opts).catch(e => e);
results.push(resp);
}
return results;
}
CheckSitesV1().then(console.log)