Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/richie-south/mulig
Resolve multible promises with order: fastest, queue, stack
https://github.com/richie-south/mulig
npm ordered promise
Last synced: about 5 hours ago
JSON representation
Resolve multible promises with order: fastest, queue, stack
- Host: GitHub
- URL: https://github.com/richie-south/mulig
- Owner: richie-south
- Created: 2017-03-01T17:54:01.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-25T11:26:12.000Z (over 7 years ago)
- Last Synced: 2024-10-12T00:54:59.598Z (about 1 month ago)
- Topics: npm, ordered, promise
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/mulig
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Mulig
[![npm version](https://badge.fury.io/js/mulig.svg)](https://badge.fury.io/js/mulig)
Resolves multible promises with order (sequence): fastest, queue or stack.
# Installation
```
npm install mulig --save
```# Usage
## Summary
* [mulig](#mulig)
* [queue](#queue)
* [stack](#stack)## mulig
`mulig()`
mulig runs callback function as soon as promises resolve. You can return values from callback functions (success or error) and they will be passed with the prev param the next time callback is invoked.
**simple example**
```javascript
const mulig = requre('mulig')/**
* resolves promises in order of completion
* @param {Array} [array of promise]
* @param {Function} [resolve function: runs every time a promise resolves]
* @param {Function} [error function: runs every time a promise throws an error]
* @return {Array} [returns given array]
*/
mulig(
[], /* array of Promises */
(
value, /* any: value of resolved promise */
index, /* Number: index of resolved promise */
isDone, /* Boolean: is last promise to resolve */
prev /* any: returned value from last time this or error function run */
) => {}, /* success function */
(
error, /* Error message */
index,
isDone,
prev
) => {} /* fail function: called on each error (optional to ignore errors)*/
)
```**complex example**
```javascriptconst mulig = requre('mulig')
// Only for explaining purpose
// takes value and resolves that value in a given time
const getPromise = (value, time) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(value)
}, time)
})// Array of promises
const promises = [
getPromise(1, 400),
getPromise(2, 1600),
getPromise(3, 1000),
getPromise(4, 2200),
]mulig(
promises,
(value, index, isDone, prev = 0) => {
console.log(`order of completion: &{value}`)
if(isDone){
console.log(`done: &{value + prev}`) // >>> 'done: 10'
}return value + prev
},
(error) => {
console.log('on promise error', error)
}
)
```## queue
`mulig.queue()`
mulig.queue resolves all promises in order (sequence) of given array.
If a promise at index 2 (in array) resolves before promise at index 0 it wont call the success or error callback before promise at index 0 and 1 resolves.**simple example**
```javascript
const mulig = requre('mulig')/**
* resolves promises in order of given array
* returns given promises
* @param {Array} [array of promise]
* @param {Function} [resolve function, this runs when a promise resolves]
* @param {Function} [error function, this runs when a promise throws an error]
* @return {Array} [returns given array]
*/
mulig.queue(
[], /* array of Promises */
(
value, /* any: value of resolved promise */
index, /* Number: index of resolved promise */
isDone, /* Boolean: is last promise to resolve */
prev /* any: returned value from last time this or error function run */
) => {}, /* success function */
(
error, /* Error message */
index,
isDone,
prev
) => {} /* fail function: called on each error (optional to ignore errors)*/
)
```**complex example**
```javascriptconst mulig = requre('mulig')
// Only for explaining purpose
// takes value and resolves that value of given time
const getPromise = (value, time) =>
new Promise((resolve) => {
setTimeout(() => {
resolve(value)
}, time)
})// Array of promises
const promises = [
getPromise(1, 400),
getPromise(2, 1600),
getPromise(3, 1000),
getPromise(4, 2200),
]mulig.queue(
promises,
(value, index, isDone, prev = 0) => {
console.log(`order of promise array: &{value}`)
if(isDone){
console.log(`done: &{prev}`) // >>> 'done: 10'
}return value + prev
},
(error) => {
console.log('on promise error', error)
}
)
```## stack
`mulig.stack()`
same as mulig.queue() but resolve in order of reversed promise array
**simple example**
```javascript
const mulig = requre('mulig')mulig.stack(
[], /* array of Promises */
(
value, /* any: value of resolved promise */
index, /* Number: index of resolved promise */
isDone, /* Boolean: is last promise to resolve */
prev /* any: returned value from last time this or error function run */
) => {}, /* success function */
(
error, /* Error message */
index,
isDone,
prev
) => {} /* fail function: called on each error (optional to ignore errors)*/
)
```