Methods helping with promises (Promise/A+ model). Not a promise shim.
- Source
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.
Name | Type | Description |
---|---|---|
promiseFuncs | Array.<functions> | The list of functions that each return a promise. |
- Source
The result the last promise resolves to.
- Type:
- *
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).
Name | Type | Description |
---|---|---|
func | function | The callback function to convert. |
- Source
The converted asyncronous function.
- Type:
- function
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.
Name | Type | Description |
---|---|---|
func | function | The callback function to convert. |
- Source
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()
.
- Source
- Type:
- Object
(async, inner) delay(ms, resolveVal)
Like Promise.resolve(resolveVal)
but waits for ms
milliseconds before resolving
Name | Type | Description |
---|---|---|
ms | number | The duration to delay the execution in milliseconds. |
resolveVal | * | The value to resolve to. |
- Source
(async, inner) delayReject(ms, rejectedVal)
like promise.delay
but rejects instead of resolving.
Name | Type | Description |
---|---|---|
ms | number | The duration to delay the execution in milliseconds. |
rejectedVal | * | The value to reject. |
- Source
(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.
Name | Type | Description |
---|---|---|
promiseGenFns | Array.<function()> | A list of functions that each return a promise. |
parallelLimit | number | The maximum number of promises to process at the same time. |
- Source
(inner) promise(obj) → {Promise}
Promise object / function converter
Name | Type | Description |
---|---|---|
obj | object | | The value or function to convert into a promise. |
- Source
- Type:
- Promise
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.
Name | Type | Description |
---|---|---|
promise | Promise | The promise to convert. |
finallyFn | function | The callback to run after either resolve or reject has been run. |
- Source
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.
Name | Type | Description |
---|---|---|
ms | number | The duration to wait for the promise to resolve in milliseconds. |
promise | Promise | The promise to wait for to finish. |
- Source
(async, inner) timeToRun(prom) → {number}
For a given promise, computes the time it takes to resolve.
Name | Type | Description |
---|---|---|
prom | Promise | The promise to time. |
- Source
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.
Name | Type | Attributes | Description |
---|---|---|---|
ms | number | <optional> | The maximum number of milliseconds to wait for |
tester | function | The function to test for the condition. | |
timeoutObj | object | <optional> | The object to resolve to if the condition was not met and we timed out. |
- Source
- Type:
- object