How To Correctly Resolve A Promise Within Promise Constructor
const setTimeoutProm = (delay) => new Promise(res => setTimeout(() => res(delay),delay)) I want to do something like, const asyncOpr = (delay) => { return new Promi
Solution 1:
No, actually the way you do it is an antipattern.
You can just return a promise from the function:
constasyncOpr = (delay) => {
return setTimeoutProm(delay);
};
If needed, a Promise could also be returned from inside a .then
:
doA()
.then(() =>setTineoutProm(1000))
.then(() =>doB());
Or it can also be awaited inside an async function:
asyncfunctionasyncOpr(delay) {
//...await setTimeoutProm(delay);
//...
}
Post a Comment for "How To Correctly Resolve A Promise Within Promise Constructor"