poll-until-promise
Advanced tools
Comparing version
@@ -264,18 +264,2 @@ import { IWaitForOptions, PollUntil, waitFor } from '../src'; | ||
it('should use an external setTimeout module', (done) => { | ||
shouldHaltPromiseResolve = true; | ||
tryingAttemptsRemaining = 2; | ||
const pollUntil = new PollUntil({ setTimeoutFunction: setTimeout }); | ||
pollUntil | ||
.tryEvery(1) | ||
.stopAfter(options.timeout!) | ||
.execute(someRandPromise) | ||
.then((value) => { | ||
expect(value).toEqual(true); | ||
done(); | ||
}); | ||
}); | ||
it('wait for within wait for should throw a single error', async () => { | ||
@@ -282,0 +266,0 @@ const options1 = { |
export declare type IExecuteFunction = any; | ||
export interface IWaitForOptions { | ||
PromiseModule?: PromiseConstructor; | ||
setTimeoutFunction?: any; | ||
interval?: number; | ||
@@ -13,4 +11,2 @@ timeout?: number; | ||
export declare class PollUntil { | ||
private readonly _PromiseModule; | ||
private readonly _setTimeoutFunction; | ||
_interval: number; | ||
@@ -32,3 +28,3 @@ _timeout: number; | ||
private _lastError; | ||
constructor({ PromiseModule, setTimeoutFunction, interval, timeout, stopOnFailure, verbose, backoffFactor, message, }?: IWaitForOptions); | ||
constructor({ interval, timeout, stopOnFailure, verbose, backoffFactor, message, }?: IWaitForOptions); | ||
tryEvery(interval: number): PollUntil; | ||
@@ -35,0 +31,0 @@ stopAfter(timeout: number): PollUntil; |
@@ -29,5 +29,3 @@ "use strict"; | ||
class PollUntil { | ||
constructor({ PromiseModule = global.Promise, setTimeoutFunction, interval = 100, timeout = 1000, stopOnFailure = false, verbose = false, backoffFactor = 1, message = '', } = {}) { | ||
this._PromiseModule = PromiseModule; | ||
this._setTimeoutFunction = setTimeoutFunction; | ||
constructor({ interval = 100, timeout = 1000, stopOnFailure = false, verbose = false, backoffFactor = 1, message = '', } = {}) { | ||
this._interval = interval; | ||
@@ -77,3 +75,3 @@ this._timeout = timeout; | ||
_applyPromiseHandlers() { | ||
this.promise = new this._PromiseModule((resolve, reject) => { | ||
this.promise = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
@@ -92,8 +90,3 @@ this.reject = reject; | ||
this._interval *= this._backoffFactor; | ||
if (typeof this._setTimeoutFunction === 'function') { | ||
this._setTimeoutFunction(this._runFunction.bind(this), this._interval); | ||
} | ||
else { | ||
setTimeout(this._runFunction.bind(this), this._interval); | ||
} | ||
setTimeout(this._runFunction.bind(this), this._interval); | ||
} | ||
@@ -100,0 +93,0 @@ _failedToWait() { |
{ | ||
"name": "poll-until-promise", | ||
"version": "4.0.3", | ||
"version": "4.0.4", | ||
"description": "Try repeatedly for a promise to be resolved", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -16,3 +16,2 @@ [![Build Status][travis-image]][travis-url] | ||
### Fetching data | ||
@@ -29,2 +28,35 @@ ```js | ||
### Using async | ||
```js | ||
import { waitFor } from 'poll-until-promise'; | ||
async function waitForDomElement(cssSelector = 'div') { | ||
try { | ||
const element = await waitFor(() => { | ||
const element = window.document.querySelector(cssSelector); | ||
if (!element) throw new Error(`failed to find element: ${cssSelector}`); | ||
return element; | ||
}, { timeout: 60_000 }); | ||
return element; | ||
} catch (e) { | ||
console.error('faled to find dom element:', e); | ||
throw e; | ||
} | ||
} | ||
async function retryFetch(path = '/get-data') { | ||
try { | ||
const data = await waitFor(async () => { | ||
const res = await fetch(path); | ||
return res.json(); | ||
}, { timeout: 60_000, interval: 1000 }); | ||
} catch (e) { | ||
console.error('faled to fetch:', e); | ||
throw e; | ||
} | ||
} | ||
``` | ||
### Waiting for something to be successful | ||
@@ -100,3 +132,3 @@ ```js | ||
## Another Example - Static Function | ||
## Static Function | ||
@@ -122,5 +154,2 @@ ```js | ||
## Used in AngularJs | ||
An AngularJs compatible library based on `poll-until-promise` [angular-wait-until](https://github.com/AlonMiz/angular-wait-until). | ||
[travis-url]: https://travis-ci.org/AlonMiz/poll-until-promise | ||
@@ -127,0 +156,0 @@ [travis-image]: https://travis-ci.org/AlonMiz/poll-until-promise.svg?branch=master |
@@ -22,4 +22,2 @@ const ERRORS = { | ||
export interface IWaitForOptions { | ||
PromiseModule?: PromiseConstructor | ||
setTimeoutFunction?: any | ||
interval?: number | ||
@@ -35,4 +33,2 @@ timeout?: number | ||
export class PollUntil { | ||
private readonly _PromiseModule: PromiseConstructor; | ||
private readonly _setTimeoutFunction: any; | ||
_interval: number; | ||
@@ -56,4 +52,2 @@ _timeout: number; | ||
constructor({ | ||
PromiseModule = global.Promise, | ||
setTimeoutFunction, | ||
interval = 100, | ||
@@ -66,4 +60,2 @@ timeout = 1000, | ||
}:IWaitForOptions = {}) { | ||
this._PromiseModule = PromiseModule; | ||
this._setTimeoutFunction = setTimeoutFunction; | ||
this._interval = interval; | ||
@@ -124,3 +116,3 @@ this._timeout = timeout; | ||
_applyPromiseHandlers() { | ||
this.promise = new this._PromiseModule((resolve, reject) => { | ||
this.promise = new Promise((resolve, reject) => { | ||
this.resolve = resolve; | ||
@@ -142,7 +134,3 @@ this.reject = reject; | ||
this._interval *= this._backoffFactor; | ||
if (typeof this._setTimeoutFunction === 'function') { | ||
this._setTimeoutFunction(this._runFunction.bind(this), this._interval); | ||
} else { | ||
setTimeout(this._runFunction.bind(this), this._interval); | ||
} | ||
setTimeout(this._runFunction.bind(this), this._interval); | ||
} | ||
@@ -149,0 +137,0 @@ |
159
22.31%54343
-1.48%838
-4.12%