Methods helping with promises (Promise/A+ model). Not a promise shim.

Methods

(async, inner) chain(promiseFuncs) → {*}

Similar to Promise.all but takes a list of promise-producing functions (instead of Promises directly) that are run sequentially. Each function gets the result of the previous promise and a shared "state" object passed in. The function should return either a value or a promise. The result of the entire chain call is a promise itself that either resolves to the last returned value or rejects with an error that appeared somewhere in the promise chain. In case of an error the chain stops at that point.

Parameters:
NameTypeDescription
promiseFuncsArray.<functions>

The list of functions that each return a promise.

Returns:

The result the last promise resolves to.

Type: 
*
Example
lively.lang.promise.chain([
  () => Promise.resolve(23),
  (prevVal, state) => { state.first = prevVal; return prevVal + 2 },
  (prevVal, state) => { state.second = prevVal; return state }
]).then(result => console.log(result));
// => prints {first: 23,second: 25}

(inner) convertCallbackFun(func) → {function}

Takes a function that accepts a nodejs-style callback function as a last parameter and converts it to a function not taking the callback but producing a promise instead. The promise will be resolved with the first non-error argument. nodejs callback convention: a function that takes as first parameter an error arg and second+ parameters are the result(s).

Parameters:
NameTypeDescription
funcfunction

The callback function to convert.

Returns:

The converted asyncronous function.

Type: 
function
Example
var fs = require("fs"),
    readFile = promise.convertCallbackFun(fs.readFile);
readFile("./some-file.txt")
  .then(content => console.log(String(content)))
  .catch(err => console.error("Could not read file!", err));

(inner) convertCallbackFunWithManyArgs(func) → {function}

Like convertCallbackFun but the promise will be resolved with the all non-error arguments wrapped in an array.

Parameters:
NameTypeDescription
funcfunction

The callback function to convert.

Returns:

The converted asyncronous function.

Type: 
function

(inner) deferred() → {Object}

Returns an object that conveniently gives access to the promise itself and its resolution and rejection callback. This separates the resolve/reject handling from the promise itself. Similar to the deprecated Promise.defer().

Returns:
Type: 
Object

(async, inner) delay(ms, resolveVal)

Like Promise.resolve(resolveVal) but waits for ms milliseconds before resolving

Parameters:
NameTypeDescription
msnumber

The duration to delay the execution in milliseconds.

resolveVal*

The value to resolve to.

(async, inner) delayReject(ms, rejectedVal)

like promise.delay but rejects instead of resolving.

Parameters:
NameTypeDescription
msnumber

The duration to delay the execution in milliseconds.

rejectedVal*

The value to reject.

(inner) parallel(promiseGenFns, parallelLimit)

Starts functions from promiseGenFns that are expected to return a promise Once parallelLimit promises are unresolved at the same time, stops spawning further promises until a running promise resolves.

Parameters:
NameTypeDescription
promiseGenFnsArray.<function()>

A list of functions that each return a promise.

parallelLimitnumber

The maximum number of promises to process at the same time.

(inner) promise(obj) → {Promise}

Promise object / function converter

Parameters:
NameTypeDescription
objobject | function

The value or function to convert into a promise.

Returns:
Type: 
Promise
Example
promise("foo");
  // => Promise({state: "fullfilled", value: "foo"})
lively.lang.promise({then: (resolve, reject) => resolve(23)})
  // => Promise({state: "fullfilled", value: 23})
lively.lang.promise(function(val, thenDo) { thenDo(null, val + 1) })(3)
  // => Promise({state: "fullfilled", value: 4})

(inner) promise_finally(promise, finallyFn) → {Promise}

Converts a given promise to one that executes the finallyFn regardless of wether it resolved successfully or failed during execution.

Parameters:
NameTypeDescription
promisePromise

The promise to convert.

finallyFnfunction

The callback to run after either resolve or reject has been run.

Returns:

The converted promise.

Type: 
Promise

(async, inner) timeout(ms, promise)

Takes a promise and either resolves to the value of the original promise when it succeeds before ms milliseconds passed or fails with a timeout error.

Parameters:
NameTypeDescription
msnumber

The duration to wait for the promise to resolve in milliseconds.

promisePromise

The promise to wait for to finish.

(async, inner) timeToRun(prom) → {number}

For a given promise, computes the time it takes to resolve.

Parameters:
NameTypeDescription
promPromise

The promise to time.

Returns:

The time it took the promise to finish in milliseconds.

Type: 
number

(inner) waitFor(msopt, tester, timeoutObjopt) → {object}

Tests for a condition calling function tester until the result is truthy. Resolves with last return value of tester. If ms is defined and ms milliseconds passed, reject with timeout error if timeoutObj is passed will resolve(!) with this object instead of raise an error This function has a huge performance impact if used carelessly. Always consider this to be the absolute last resort if a problem can not be solved by promises/events.

Parameters:
NameTypeAttributesDescription
msnumber<optional>

The maximum number of milliseconds to wait for tester to become true.

testerfunction

The function to test for the condition.

timeoutObjobject<optional>

The object to resolve to if the condition was not met and we timed out.

Returns:
Type: 
object