I'm writing a node.js function that returns a different promise depending on a condition, the cod:
if(condition){
return promise.then(() => {
return Promise.resolve(value)
})
}else{
return anotherPromise
}
Now the problem is that if the condition is true, I need to something after the promise is fulfilled, but in the other case I just return the promise, so the eslint tells me that it's a bad practice to nest promises. So this code won't work for me:
(() => {
if(condition){
return promise
}
}else{
return anotherPromise
}
}).then(() => {
return Promise.resolve(value)
})
Because using this code the then callback will be executed in the two cases.
What is the best practice to handle this case?
async/awaitan option?Promise.resolve(value)above really part of the code or just a placeholder for some other logic? If it's part of the code, why don't you just returnvalueat that point? there is no need to wrap this actually.