-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Luke Batchelor <[email protected]>
- Loading branch information
1 parent
a112539
commit c2093ce
Showing
5 changed files
with
57 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
language: node_js | ||
node_js: | ||
- '8' | ||
- '6' | ||
- '4' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,34 @@ | ||
'use strict'; | ||
module.exports = (condition, interval) => new Promise((resolve, reject) => { | ||
interval = typeof interval === 'number' ? interval : 20; | ||
const pTimeout = require('p-timeout'); | ||
|
||
const check = () => { | ||
Promise.resolve().then(condition).then(val => { | ||
if (typeof val !== 'boolean') { | ||
throw new TypeError('Expected condition to return a boolean'); | ||
} | ||
module.exports = (condition, opts) => { | ||
opts = Object.assign({ | ||
interval: 20, | ||
timeout: Infinity | ||
}, opts); | ||
const promise = new Promise((resolve, reject) => { | ||
const check = () => { | ||
Promise.resolve().then(condition).then(val => { | ||
if (typeof val !== 'boolean') { | ||
throw new TypeError('Expected condition to return a boolean'); | ||
} | ||
|
||
if (val === true) { | ||
resolve(); | ||
} else { | ||
setTimeout(check, interval); | ||
} | ||
}).catch(reject); | ||
}; | ||
if (val === true) { | ||
resolve(); | ||
} else { | ||
setTimeout(check, opts.interval); | ||
} | ||
}).catch(err => { | ||
reject(err); | ||
}); | ||
}; | ||
|
||
check(); | ||
}); | ||
check(); | ||
}); | ||
|
||
if (opts.timeout !== Infinity) { | ||
return pTimeout(promise, opts.timeout); | ||
} | ||
|
||
return promise; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,5 +40,8 @@ | |
}, | ||
"xo": { | ||
"esnext": true | ||
}, | ||
"dependencies": { | ||
"p-timeout": "^2.0.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters