Description
this-promise
is a module that creates the Promise chain of [Array]
list of promises with its own inner userContext this
context.
- Any bugs found? Give me to know on dev.rafalko@gmail.com or on GitHub
Installation
npm install this-promise
const thisPromise = ;
Idea
- The
this-promise
function allows you to create the chain of promises by passing all promises as the [Array] list through the module function, what may turn out to be more elegant and readable in some cases. - Under the hood, the native
Promise
object with itsthen
andcatch
methods is used. - It creates dynamically the chain of
then()
andcatch()
methods. The promises are called back-to-back, as if they were called manually withPromise.resolve().then(a).then(b).then(c).catch(err)
etc. - It is similar to
Promise.all
andPromise.race
built-in methods, where the iterable object is passed through it as an argument with the promises (or other values). - Each
then
andcatch
handler is binded to the inner userContextthis
object. It allows all promises to get and set the data usingthis
keyword, rather than pass the data throughresolve(data)
andreject(data)
callback functions to the further promises.
Usage
thisPromise(list[,finalThen[,finalCatch]])
list
[Array]
- The [Array] list should contain the items that will be chained with Promise
then
method. - The items should be of [Function] type. They are automatically passed as Promise
then(item)
's handlers - The [Function] item can return the [Promise] object. Then the item will be queued in the chain and the further items will be suspended till the fulfilment or rejection of this item. [see below]
- The items can be also of any type. The value is automatically encapsulated (returned) in the function and passed as resolved Promise (send to the further promise) [see below]
finalThen
[Function] optional
- The [Function]
finalThen
is automatically attached at the end of the chain asthen()
function and executed as the last promise (if all previous promises succeeded) [see below]
finalCatch
[Function] optional
- The [Function]
finalCatch
is automatically attached at the end of the chain ascatch()
function and executed in case of the rejection of any of the promises. [see below]
Return
The thisPromise
function returns the [Promise] object, so further then
s and catch
es can be attached to it [see below]
Samples
this
keyword is used in the handlers.
Mind how Mind how promises are passed as [Array] items.
const thisPromise = ; ; { //set some initial data to the this userContext object //that will be accessible in each chained promise thisisCoder = true; thisisHuman = true; thisexperience = 5;} { return { ; };} { return { ; };} { console; ifthisisHuman console; ifthisisCoder console;} { console;}
finalThen
and finalCatch
parameters are optional.
The They are attached to the chain only if they are of [Function] type.
const thisPromise = ; ; { return { ; };} { console;}
The [Array] item can be of any type
It is automatically encapsulated (returned) in the function
const thisPromise = ; ;//the {name:'John Doe',age:25} is automatically encapsupated in the function//function(){ return {name:'John Doe',age:25}; }//and passed through then() method { //now the {name:'John Doe',age:25} object is accessible as getData parameter thisname = getDataname; thisage = getDataage;} { console; //Hello, I'm John Doe and I'm 25 years old.} { console;}
contextPromise
module returns the Promise, so the further then
s and catch
es can be attached
The this
object is inaccessible in outer then
s and catch
es
Unfortunately the userContext const thisPromise = ; ; { return { ; };}