Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including q-util with all npm packages installed. Try it out:

var qUtil = require("q-util")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

q-util v0.0.2

Construction zone for a Q promise composition library.

A construction zone for a Q promise composition library. All names subject to change. May explode in your face.

Exports all of the "q" API for convenience in addition to:

delay(timeout)
    returns a promise that will resolve (to undefined)
    after timeout miliseconds.

forEach(values, callback, that)
    serially executes the callback, regardless
    of the order and timing of the resolution of each
    given value, returning a promise (for undefined) that
    will resolve when the entire sequence has been
    completed.

map(values, callback, that)
    executes a callback in parallel on each value and
    returns an array of promises.

reduce(values, callback(accumulated, value), accumulated_opt)
    oportunistically reduces the values given in the 
    order in which they are resolved, returning a promise
    for the final accumulation.

reduceLeft(values, callback(accumulated, value), accumulated_opt)
    serially executes the callback, regardless
    of the order and timing of the resolution of each
    given value, returning a promise for the ultimately
    accumulated value.

reduceRight(values, callback(accumulated, value), accmulated_opt)

loop(producer, callback)

shallow(values)
    - takes an array of values.  the array may be promised.
      each value may be promised.
    - returns a promise for an array of the respective fully
      resolved values.
    - the returned promise will be rejected if any of
      the contained promises are rejected.

deep(object)
    returns a promise for the deep resolution of an object.
    That is, it accepts an object that can
    1. be a promise
    2. be an array or object containing promises
    3. be a promise that resolves to an array or object
       of promises
    4. all of the above, recursively
    and returns a promise that resolves to a value that
    is a tree of the corresponding resolved values.

    Q.when(Q.deep({
        "delays": UTIL.range(3).map(function (n) {
            return Q.when(Q.delay(n * 1000), function () {
                return n;
            });
        })
    }), function (thing) {
        // 3 seconds later (not 2, not 1, not 6)
        ASSERT.deepEqual(thing, {
            "delays": [1, 2, 3]
        })
    });

chain(f, g, h, ...)

    Calls each function in order with the resolved return
    value of the previous. 

    `Q.chain(f, g, h)` is equivalent to
    `[f, g, h].reduce(Q.when, undefined)`

sum(values)
    returns a promise for the sum of each value when
    it resolves.

retry({
    delay: 1000,
    max: Inifinity,
    backOff: 2,
    onretry: callback
}, callback)
    makes multiple attempts at resolving a value, returned by a
    given callback, applying exponential back-off.

Queue(max)
    returns a promise queue with an optional maximum
    size.  A promise queue has get and put methods.
    A promise queue can produce promises for values
    that have not yet been put on the queue.

    get()
        returns a promise.  the promise will already
        have been resolved if more values have been put
        than gotten.

    put(value)
        resolves a promise: the next in the order
        of gotten promises.

Buffer()

    get()
        gets a promise from the buffer, even for a value
        that has not yet been put.
    put()
        adds a value to the buffer, resolving the next
        outstanding gotten promise.
    flush()
        returns a promise for an array of put values
        whenever one or more values are available.
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free