Promise Ngine
A promise engine to resolve async objects with concurrency and serial resolution on demand passing the result along the way.
Quick example:
Promise
How to use :
Without binding to global Promise object
const PromiseNgine = new ;
Binding to global Promise object
const PromiseNgine = new true;
All examples will use the method binding to global Promise object.
The engine exposes two methods :
1. Promise.ngine
A method resolving all ngine keys contained in an object or an array passing the result.
Promise.NgineKey
An engine key is constructed with a method taking three arguments as below :
{};
An engine key can be resolved calling the resolve method passing the result that will be mapped to the key's original position in the object. It can also be resolved returning a Promise.
The property pos
on a engine key sets the position of the Promise in the chain. 0 being first position.
{};
Examples
Example with a pseudo sql call and a pseudo mongo call needing result from sql :
Promise
To have the 'orders' promise access the result of the 'infos' promise,
the integer passed to .pos
method of the 'orders' promise must be higher than the one for the 'infos' promise.
Concurrency
At the same time, you might need other data that don't need anything from the 'orders' promise but needs data from the 'infos' promise. Therefore, it can run concurrently with the 'orders' promise. This can be achieved by passing the same position as the one passed to the 'orders' promise.
Example:
Promise
So every promise sharing the same position wherever it is in the object will run concurrently. Therefore if a promise relies on the result of another one, they must not share the same position.
Copying the original object
Useful if you want to reuse the original object.
let obj =somePromise:;Promise
Subobject
An engine key can return another Promise.ngine. The sub ngine will have its own chain.
Example:
Promise
Result available as this
the resulting object is also available with this inside an ngine key. In order for it to work do not use fat arrows functions.
Example:
Promise
It works with unlimited depth. It works with arrays (as first argument also).