How do I convert an existing callback API to promises?

Promises have state, they start as pending and can settle to: fulfilled meaning that the computation completed successfully. rejected meaning that the computation failed. Promise returning functions should never throw, they should return rejections instead. Throwing from a promise returning function will force you to use both a } catch { and a .catch. People … Read more

What is the explicit promise construction antipattern and how do I avoid it?

The deferred antipattern (now explicit-construction anti-pattern) coined by Esailija is a common anti-pattern people who are new to promises make, I’ve made it myself when I first used promises. The problem with the above code is that is fails to utilize the fact that promises chain. Promises can chain with .then and you can return … Read more

How to wait until promise finishes before continuing the loop

var p = promiseReturnedFromAFunction(); var time = new Date().getTime(); var user=”undefined”; p.then(() => { while(user === ‘undefined’ && Math.abs(time – new Date().getTime()) < 10000) { if(somecondition) { user=”user”; break; } } }); I think you have it inverted. You want to wait for the promise to resolve, then loop through your condition checks to make … Read more