Skip to content

Commit

Permalink
Abstracts value check to isPromise
Browse files Browse the repository at this point in the history
(to make it easier to follow)
  • Loading branch information
andyfleming authored Jan 14, 2020
1 parent e7d89ae commit a099d07
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
/**
* @param {*} val - value to check
*
* @returns {boolean} true if the value is then-able
*/
const isPromise = val => (val != null && typeof val.then === "function")

/**
* @param {function} func - function to execute
* @param {number|function(number):number} intervalLength - length in ms to wait before executing again
Expand Down Expand Up @@ -53,7 +60,7 @@ function interval(func, intervalLength, options = {}) {

const returnVal = func(currentIteration, stop)
// Any valid Promise implementation must have a `then` method
if (!(returnVal != null && typeof returnVal.then === "function")) {
if (!isPromise(returnVal)) {
rootPromiseReject(new Error('Return value of "func" must be a Promise.'))
return
}
Expand Down

0 comments on commit a099d07

Please # to comment.