Introduction
Sequential promises chain
Often you need to resolve a promises chain in order as a sequential chain.
Getting started
Install module:
$ npm i sequential-promises-chain
Install and save module in your project:
$ npm i -S sequential-promises-chain
API
Create new instance:
const SequentialPromisesChain = const spc = ;
Resolve a promises chain:
//Array of elements on wich the function may be make some operationsconst array = ...//A function that operate on elements and return a promiseconst predicate = { return { ... return or return ... }} try // Execute the function on each array element as a sequential chain /** * resolve * @param * @param * The function get as parameters: * element - array element in order * index - the index of the current element * @param * @returns Array of ordered promises - The resolve start when this function is called */ const res = await spc; // return Promise // Await the resultcatcherr ...
You can also specify an iteratee function:
const spc = ; const users = name: 'Jonathan' name: 'Toni' name: 'Nicola' const predicate = { return { userid = index }} const iteratee = { return user} const final = await spc // expect(final.length).toBe(3)// expect(final[0].name).toBe(users[0].name)// expect(final[0].id).toBe(0)// expect(final[1].name).toBe(users[1].name)// expect(final[1].id).toBe(1)// expect(final[2].name).toBe(users[2].name)// expect(final[2].id).toBe(2)
By default the Promises chain will stop if an error occur, but you can specify force: true
to continue exec of Promises even if one fail:
const spc = force: true; const users = name: 'Jonathan' name: 'Toni' name: 'Nicola' const predicate = { return { userid = index }} const iteratee = { return user} try const final = await spc // expect(final.success.length).toBe(2) // expect(final.fail.length).toBe(1) catch error // expect(error).toBeUndefined()
See tests folder for more examples
Feel free to open issues, fork project, and collaborate with us!
Contribute
Clone repository locally and install dependencies:
$ git clone https://github.com/daton89-topperblues/sequential-promises-chain$ cd sequential-promises-chain$ npm i
Fork project and open pull request
Contributors
@topperblues Nicola Bonavita
@daton89 Toni D'Angelo