poll-until-promise
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("PollUntil",[],e):"object"==typeof exports?exports.PollUntil=e():t.PollUntil=e()}(this,(function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){(function(e){const i="Your executor is not a function. functions and promises are valid.",r="Failed to wait";class s{constructor({Promise:t=e.Promise,setTimeout:i,interval:r=100,timeout:s=1e3,stopOnFailure:o=!1,verbose:n=!1,message:u}={}){this._PromiseModule=t,this._setTimeoutModule=i,this._interval=r,this._timeout=s,this._stopOnFailure=o,this._isWaiting=!1,this._isResolved=!1,this._verbose=n,this._userMessage=u,this.originalStacktraceError=new Error,this._Console=console}tryEvery(t){return this._interval=t,this}stopAfter(t){return this._timeout=t,this}execute(t){return this._executeFn=t,this._applyPromiseHandlers(),this._validateExecution(),this.start=Date.now(),this._isWaiting=!0,this._log("starting to execute"),this._runFunction(),this.promise}getPromise(){return this.promise}isResolved(){return this._isResolved}isWaiting(){return this._isWaiting}stopOnFailure(t){return this._stopOnFailure=t,this}_applyPromiseHandlers(){this.promise=new this._PromiseModule((t,e)=>{this.resolve=t,this.reject=e})}_validateExecution(){if("function"!=typeof this._executeFn)throw new Error(i)}_timeFromStart(){return Date.now()-this.start}_shouldStopTrying(){return this._timeFromStart()>this._timeout}_executeAgain(){this._log("executing again"),"function"==typeof this._setTimeoutModule?this._setTimeoutModule(this._runFunction.bind(this),this._interval):setTimeout(this._runFunction.bind(this),this._interval)}_failedToWait(){let t=`${r} after ${this._timeFromStart()}ms`;return this._userMessage&&(t=`${t}: ${this._userMessage}`),this._lastError?this._lastError.message=`${t}\n${this._lastError.message}`:(this._lastError=this.originalStacktraceError,this._lastError.message=t),this._log(this._lastError),this._lastError}_runFunction(){if(this._shouldStopTrying())return this._isWaiting=!1,void this.reject(this._failedToWait());let t=this._executeFn();"object"==typeof t&&"function"==typeof t.then||(t=new this._PromiseModule(e=>e(t))),t.then(t=>{if(!1===t)return this._log(`then execute again with result: ${t}`),void this._executeAgain();this.resolve(t),this._isWaiting=!1,this._isResolved=!0,this._log(`then done waiting with result: ${t}`)}).catch(t=>this._stopOnFailure?(this._log(`stopped on failure with err: ${t}`),this.reject(t)):(this._lastError=t,this._log(`catch with err: ${t}`),this._executeAgain()))}_log(t){this._verbose&&this._Console&&this._Console.log&&this._Console.log(t)}}t.exports={PollUntil:s,waitFor:(t,e)=>new s(e).execute(t)}}).call(this,i(2))},function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i}])})); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define("PollUntil",[],e):"object"==typeof exports?exports.PollUntil=e():t.PollUntil=e()}(this,(function(){return function(t){var e={};function i(r){if(e[r])return e[r].exports;var s=e[r]={i:r,l:!1,exports:{}};return t[r].call(s.exports,s,s.exports,i),s.l=!0,s.exports}return i.m=t,i.c=e,i.d=function(t,e,r){i.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},i.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},i.t=function(t,e){if(1&e&&(t=i(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(i.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var s in t)i.d(r,s,function(e){return t[e]}.bind(null,s));return r},i.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return i.d(e,"a",e),e},i.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},i.p="",i(i.s=0)}([function(t,e,i){t.exports=i(1)},function(t,e,i){(function(e){const i="Your executor is not a function. functions and promises are valid.",r="Failed to wait";class s{constructor({Promise:t=e.Promise,setTimeout:i,interval:r=100,timeout:s=1e3,stopOnFailure:o=!1,verbose:n=!1,message:u}={}){this._PromiseModule=t,this._setTimeoutModule=i,this._interval=r,this._timeout=s,this._stopOnFailure=o,this._isWaiting=!1,this._isResolved=!1,this._verbose=n,this._userMessage=u,this.originalStacktraceError=new Error,this._Console=console}tryEvery(t){return this._interval=t,this}stopAfter(t){return this._timeout=t,this}execute(t){return this._executeFn=t,this._applyPromiseHandlers(),this._validateExecution(),this.start=Date.now(),this._isWaiting=!0,this._log("starting to execute"),this._runFunction(),this.promise}getPromise(){return this.promise}isResolved(){return this._isResolved}isWaiting(){return this._isWaiting}stopOnFailure(t){return this._stopOnFailure=t,this}_applyPromiseHandlers(){this.promise=new this._PromiseModule((t,e)=>{this.resolve=t,this.reject=e})}_validateExecution(){if("function"!=typeof this._executeFn)throw new Error(i)}_timeFromStart(){return Date.now()-this.start}_shouldStopTrying(){return this._timeFromStart()>this._timeout}_executeAgain(){this._log("executing again"),"function"==typeof this._setTimeoutModule?this._setTimeoutModule(this._runFunction.bind(this),this._interval):setTimeout(this._runFunction.bind(this),this._interval)}_failedToWait(){let t=`${r} after ${this._timeFromStart()}ms`;if(this._userMessage&&(t=`${t}: ${this._userMessage}`),this._lastError){this._lastError.message=`${t}\n${this._lastError.message}`;const e=this.originalStacktraceError.stack;this._lastError.stack+=e.substring(e.indexOf("\n")+1)}else this._lastError=this.originalStacktraceError,this._lastError.message=t;return this._log(this._lastError),this._lastError}_runFunction(){if(this._shouldStopTrying())return this._isWaiting=!1,void this.reject(this._failedToWait());let t=this._executeFn();"object"==typeof t&&"function"==typeof t.then||(t=new this._PromiseModule(e=>e(t))),t.then(t=>{if(!1===t)return this._log(`then execute again with result: ${t}`),void this._executeAgain();this.resolve(t),this._isWaiting=!1,this._isResolved=!0,this._log(`then done waiting with result: ${t}`)}).catch(t=>this._stopOnFailure?(this._log(`stopped on failure with err: ${t}`),this.reject(t)):(this._lastError=t,this._log(`catch with err: ${t}`),this._executeAgain()))}_log(t){this._verbose&&this._Console&&this._Console.log&&this._Console.log(t)}}t.exports={PollUntil:s,waitFor:(t,e)=>new s(e).execute(t)}}).call(this,i(2))},function(t,e){var i;i=function(){return this}();try{i=i||new Function("return this")()}catch(t){"object"==typeof window&&(i=window)}t.exports=i}])})); | ||
//# sourceMappingURL=poll-until-promise.js.map |
{ | ||
"name": "poll-until-promise", | ||
"version": "3.4.0", | ||
"version": "3.5.0", | ||
"description": "Try repeatedly for a promise to be resolved", | ||
@@ -5,0 +5,0 @@ "main": "lib/poll-until-promise.js", |
@@ -11,3 +11,4 @@ const ERRORS = { | ||
setTimeout, | ||
interval = 100, timeout = 1000, | ||
interval = 100, | ||
timeout = 1000, | ||
stopOnFailure = false, | ||
@@ -109,2 +110,4 @@ verbose = false, | ||
this._lastError.message = `${waitErrorText}\n${this._lastError.message}`; | ||
const originalStack = this.originalStacktraceError.stack; | ||
this._lastError.stack += originalStack.substring(originalStack.indexOf('\n') + 1); | ||
} else { | ||
@@ -111,0 +114,0 @@ this._lastError = this.originalStacktraceError; |
@@ -304,3 +304,3 @@ const { PollUntil, waitFor } = require('../src/poll-until-promise'); | ||
it('wait for should show the user message on failure', async () => { | ||
it('wait for should show the user message on failure', async (done) => { | ||
options.message = 'waiting for something' | ||
@@ -313,6 +313,7 @@ try { | ||
expect(e.message).toMatch(/^Failed to wait after \d+ms: waiting for something\nsome error message$/); | ||
done() | ||
} | ||
}); | ||
it('wait for should save the original stacktrace', async () => { | ||
it('wait for should save the original stacktrace', async (done) => { | ||
options.message = 'waiting for something' | ||
@@ -329,4 +330,32 @@ try { | ||
expect(e.stack).toMatch(/customFunction/); | ||
done() | ||
} | ||
}); | ||
it('should show stack if thrown inside a function', async (done) => { | ||
let counter = 100 | ||
try { | ||
function functionA() { | ||
return waitFor(async () => { | ||
if (counter !== 0) { | ||
counter -= 1; | ||
throw new Error('try again'); | ||
} else { | ||
console.log('all good') | ||
} | ||
}, { timeout: 20, interval: 2, verbose: true }) | ||
} | ||
async function functionB() { | ||
await functionA() | ||
} | ||
await functionB() | ||
} catch (e) { | ||
expect(e.message).toMatch(/try again/); | ||
expect(e.stack).toMatch(/functionA/); | ||
expect(e.stack).toMatch(/functionB/); | ||
done() | ||
} | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
37813
3.5%494
6.24%