What is the correct usage with Promise.all() in Nodejs?
Problem
request module is a pretty popular module in Node.js and I am using it as single pretty simple:
request.get("url.com", function (error, response, body) { console.log(body); }) |
Pretty easy to handle with.
If I have 10 requests like this, imagine
var myReq1 = request.get("url.com", function (error, response, body) { console.log(body); }) var myReq2 = request.get("url.com", function (error, response, body) { console.log(body); }) var myReq3 = request.get("url.com", function (error, response, body) { console.log(body); }) var myReq4 = request.get("url.com", function (error, response, body) { console.log(body); }) . . . |
And I want to have one callback for all when they all actually are done with their own callback function.
//body printed //body printed //body printed //body printed Promise.all([myReq1, myReq2, myReq3, ....]).then((values) => { console.log(values); // }); |
Is this the correct usage with Promise.all() to achieve what I want ? Also,1 more question is, with request npm, does a promise resolve when the callback function to that specific request starts, or when it ends ?
To make it more clear,
request("url", (callback) => { //Does the promise resolve here. console.log('promise resolved now at start!') .... console.log('promise will be resolved now after my logging') //which one correct }) |
Edit: This is a way with 1 promise I found in another thread, how to do with multiple?
function doRequest(url) { return new Promise(function (resolve, reject) { request(url, function (error, res, body) { if (!error && res.statusCode === 200) { resolve(body); } else { reject(error); } }); }); }
// Usage: async function main() { try { let response = await doRequest(url); console.log(response); // `response` will be whatever you passed to `resolve()` at the top } catch (error) { console.error(error); // `error` will be whatever you passed to `reject()` at the top } }
main(); |
Solution
Is this the correct usage with Promise.all() to achieve what I want ?
Yes, Promise.all() is the correct usage for your requirement. But "request" module won't return any promise. Wrap the response of "request module" with promise and use Promise.all(). >https://www.javascripttutorial.net/es6/javascript-promise-all/
does a promise resolve when the callback function to that specific request starts, or when it ends ?
Callback function will start only when the promise is resolved.
This is a way with 1 promise i found in another thread, how to do with multiple?
For multiple urls also you can do it in the same way.
Note: Nesting too many callbacks will lead to “callback hell” or “pyramid of doom.” Which is not recommended. Please use async and await. >https://javascript.info/async-await
// Function that do the api calls. function doRequest(url) { return new Promise(function (resolve, reject) { request(url, function (error, res, body) { if (!error && res.statusCode === 200) { resolve(body); } else { reject(error); } }); }); } |
To do the multiple API call, we can call the above "doRequest" function in 2 ways. Based on our use case.
// Usage: If we want to hit multiple APIs one after the other we can follow the below method. async function main() { try { let response1 = await doRequest(url1); // url1 let response2 = await doRequest(url2); // url2 let response3 = await doRequest(url3); // url3 console.log("Got response for all API call"); // `response` will be whatever we passed to `resolve()` at the top } catch (error) { console.error(error); // `error` will be whatever you passed to `reject()` at the top } }
main(); |
// Usage: If we want to hit multiple api's in parallel and wait till the response to comes back. we can follow the below method. async function main() { let promise1 = doRequest(url1); let promise2 = doRequest(url2); let promise3 = doRequest(url3); Promise.all([promise1, promise2, promise3]).then((values) => { console.log(values); // resolved values of all promises. This value will be logged only when all the promises are resolved. }).catch((error) => { console.log(error); // rejected value of the promise. }); } |
Answered by: >Sat21343
Credit: >StackOverflow
Blog links:
>How to add a Parsing PHP file in order to get an array of parameters?
>How to correct WordPress site errors after upgrading to PHP 8.2?
>How does Perl match a string containing a dot in PHP?
>How to use PHP to display MySQL results in an HTML table?