I have just started playing with nodeJS and I try to get familiar with promises.
I have the code bellow and for me it looks like it can be improved by moving the retry logic and put it inside getValue2.
Retry logic is different than the getValue2.
The problem is that as soon as I put the logic inside the method, getValue2 finishes before retryGetValue2's promise finishes.
Ideally, I want to remain just with line sendPriceResponse(res, res2); and get rid of the if-else
Any recommendations?
This is the code:
getValue1(link).then( function(res)
{
getValue2(link2).then(function(res2)
{
if(res2==='') // retry logic <===---------------|
{ //|
retryGetValue2(link2).then(function(res2new)//|
{ //|
sendPriceResponse(res, res2new); //|
}); //|
} //_________________|
else
{
sendPriceResponse(res, res2);
}
});
});
getValue2 looks like :
function getValue2(link)
{
return getInfo(link); // returns a promise
}