I need to create a JavaScript Promise that will not resolve until a specific condition is true. Let\'s say I have a 3rd party library, and I need to wait until a certain da
A small variation would be to use a named IIFE so that your code is a little more concise and avoids polluting the external scope:
function ensureFooIsSet() {
return new Promise(function (resolve, reject) {
(function waitForFoo(){
if (lib.foo) return resolve();
setTimeout(waitForFoo, 30);
})();
});
}