what is the best practice to use promise.all? [closed]

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



what is the best practice to use promise.all? [closed]



i have to make two calls and get consolidated response so in below code Promise.all is not being invoked any idea what is implemented wrong and i wanted to know best practice or approach in below scenario to achieve both request response using Promise.all.



I am using tsoa format in below code with typescript.



main.ts


@Post("getStoreDetails")
public async getStoreDetail(@Body() request: express.Request): Promise < any >
const stackurl = "http://staclurl"
const storeurl = "http://storeurl"



if (request.body.lob === "Stack")
const stack: any = await axios.post(stackurl, req.body).then(
function(res)
if (res.data.Header.StatusCode !== '0000')
throw res.data.Header;

const Stackresponse = res.data.Details;
return Stackresponse;
);

if (request.body.lob === "Admin")
const store: any = await axios.post(storeurl, req.body).then(
function(res)
if (res.data.Header.StatusCode !== '0000')
throw res.data.Header;

const StoreResponse = res.data.Details;
return StoreResponse;
);



return Promise.all([stack, store]);





Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.





Both of those conditions can't be true so don't see why you would consider Promise.all. One of those variables will be undefined. Then since you use await neither variable will return a promise
– charlietfl
Aug 10 at 16:08



await





With const, neither of the two variables are in scope
– Bergi
Aug 10 at 17:34


const




3 Answers
3



Your stack and store are inside a if statement, making them out of scope for the outer function. You can use an array in the function scope to store the two promises you create in the if statement. Note that you would have to remove await to get them to actually return promises that Promise.all would be able to resolve





Kind of getting what you saying can you give me some code examples ?
– hussain
Aug 10 at 16:13



Your problem is two fold:


Promise.all


await



the first mistake is that request.body.lob can only hold one value. Your Promises are inside an if statement. Therefore only one of them is getting returned.



The second mistake is that you are not really returning the promise but the value of the resolved promise because of the await keyword.


await



You are returning the value of the promise inside your stack and store variables not the promises itself. Therefore Promise.all can do nothing for you because there are not pending promises stored there.


stack


store



Solve this by storing the promise in the variables and do the conditionals in a different manner.



Seems like all you need is one request and set url based on conditional


@Post("getStoreDetails")
public async getStoreDetail(@Body() request: express.Request): Promise < any >

const urls =
'Stack': "http://staclurl",
'Admin': "http://storeurl"


const url = urls[request.body.lob];

if (url)
return await axios.post(url, req.body).then(
function(res)
if (res.data.Header.StatusCode !== '0000')
throw res.data.Header;

return res.data.Details;
);
else
// what to return if no matching url??







in future lob could also come as an array and we have to make both calls simultaneously
– hussain
Aug 10 at 17:20





any idea if we have both lobs in the request how we can achieve that with promise.all ?
– hussain
Aug 13 at 14:09

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard