promised-until
v1.0.2
Published
Transforms a (promised) value until it satisfies the given predicate.
Downloads
8
Maintainers
Readme
promised-until
Transforms a (promised) value until it satisfies the given predicate. Full support for Promises.
Usage
until(predicate, transform)(value) -> Promise(newValue)
Calling until(predicate, transform)
returns a function that takes one parameter. Calling that function with value
returns a promise, which resolves if predicate(value)
is truthy. While predicate(value)
is falsey, the value
is transformed with transform(value)
until predicate(value)
returns a truthy value.
predicate(x) -> true|false or Promise(true|false)
A function that accepts one parameter x
and returns a truthy or falsey value, or Promise of such a value.
transform(x) -> y or Promise(y)
A function that accepts one parameter x
and transforms it into a new value y
, or Promise of such a value.
value
The value, or Promise of a value to be transformed.
Example
Non-promised
const until = require('promised-until')
until(
(c) => c >= 5,
(c) => c + 1
)(0)
.then(result => console.log(result)) // logs '5' to the console
Promised
const until = require('promised-until')
until(
(c) => Promise.resolve(c >= 5),
(c) => Promise.resolve(c + 1)
)(Promise.resolve(0))
.then(result => console.log(result)) // logs '5' to the console
Real-world
until(
(pageToken) => !pageToken
(pageToken) => {
return fetchPage(pageToken)
.then(page => {
socket.send(page)
return page.nextPageToken
})
)(getFirstPageToken())
.then(() => console.log('done'))
Supported versions
The module is CI-tested with Node versions 0.10, 4.5 (LTS) and the latest (currently 6.5).
Promise implementation
Uses any-promise which defaults to the native Promise
implementation. If you want to use a different Promise library, make sure to register it with any-promise
. For example require('any-promise/register/bluebird')
for bluebird.
For Node versions <= 0.12
a 3rd-party Promise library is required. More details here.