The library which will be able to handle promises as various ways
npm install meari!npm  !npm bundle size !npm !NPM
The library which will be able to handle promises as various ways
This library because it is based only a promise constructor so is not need a browsers of higher version supporting Promise.all or Promise.allSettled functions
> For examples seq and seqAll functions will be able to replaces Promise.all and Promise.allSettled functions
In addition, it is supporting various functions like map and retry that in the native is not supports for can using in a multiple situations
javascript
npm i meari
`
Support Platforms
Most of modern browsers(chrome, edge, firefox ...) that supporting Promise, NodeJS
How to Use
$3
These features are based execution order like a
Promise.all and Promise.race of a native api
`javascript
import {
seq,
seqAll,
map,
mapAll,
race,
raceAll,
retry,
retryAll,
assert,
assertAll,
} from 'meari';let p1, p2, p3;
p1 = Promise.resolve(1);
p2 = Promise.resolve(2);
p3 = Promise.resolve(3);
seq([p1, p2, p3])
.then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{ status: 'fulfilled', value: 2, index: 1 },
{ status: 'fulfilled', value: 3, index: 2 }
]
*/
})
.catch(v => {
console.log(v);
})
.finally(() => {
console.log('finally');
});
p1 = Promise.resolve(1);
p2 = Promise.reject(new Error());
p3 = Promise.resolve(3);
seqAll([p1, p2, p3])
.then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{
status: 'rejected',
value: Error:
at Object. (/Users/user/htdocs/mohwa/async-trainer/examples/index.js:27:27)
...
index: 1
},
{ status: 'fulfilled', value: 3, index: 2 }
]
*/
})
.finally(() => {
console.log('finally');
});
p1 = Promise.resolve(1);
p2 = Promise.resolve(2);
p3 = Promise.resolve(3);
map([p1, p2, p3], ({ value }) =>
TEST_${value})
.then(values => {
console.log(values); // [ 'TEST_1', 'TEST_2', 'TEST_3' ]
})
.catch(e => {
console.dir(e);
})
.finally(() => {
console.log('finally');
});p1 = Promise.resolve(1);
p2 = Promise.reject(new Error());
p3 = Promise.resolve(3);
mapAll([p1, p2, p3], ({ status, value }) => {
if (status !== 'rejected') {
return
TEST_${value};
} else {
return value;
}
})
.then(values => {
console.log(values);
/*
[
'TEST_1',
Error:
at Object. (/Users/user/htdocs/mohwa/async-trainer/examples/index.js:68:27)
...
'TEST_3'
]
*/
})
.finally(() => {
console.log('finally');
});p1 = Promise.resolve(1);
p2 = Promise.resolve(2);
p3 = Promise.resolve(3);
race([p1, p2, p3])
.then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{ status: 'fulfilled', value: 2, index: 1 },
{ status: 'fulfilled', value: 3, index: 2 }
]
*/
})
.catch(e => {
console.dir(e);
})
.finally(() => {
console.log('finally');
});
p1 = Promise.resolve(1);
p2 = Promise.reject(new Error());
p3 = Promise.resolve(3);
raceAll([p1, p2, p3])
.then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{ status: 'fulfilled', value: 3, index: 2 },
{
status: 'rejected',
value: Error:
at Object. (/Users/user/htdocs/mohwa/async-trainer/examples/index.js:117:27)
...
index: 1
}
]
*/
})
.finally(() => {
console.log('finally 1');
});
p1 = () => Promise.resolve(1);
p2 = () => Promise.resolve(2);
p3 = () => Promise.resolve(3);
retry([p1, p2, p3], 3, 1000)
.then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{ status: 'fulfilled', value: 2, index: 1 },
{ status: 'fulfilled', value: 3, index: 2 }
]
*/
})
.catch(value => {
console.log(value);
});
p1 = () => Promise.resolve(1);
p2 = () => Promise.reject(new Error());
p3 = () => Promise.resolve(3);
retryAll([p1, p2, p3], 3, 1000).then(values => {
console.log(values);
/*
[
{ status: 'fulfilled', value: 1, index: 0 },
{
status: 'rejected',
value: Error:
at callback (/Users/user/htdocs/mohwa/async-trainer/examples/index.js:161:33)
...
index: 1
},
{ status: 'fulfilled', value: 3, index: 2 }
]
*/
});
let cb1, cb2, cb3;
cb1 = () => true;
cb2 = () => true;
cb3 = () => true;
assert([cb1, cb2, cb3], 3, 1000)
.then(values => {
console.log(values);
/*
[
{ status: 'truly', value: true, index: 0 },
{ status: 'truly', value: true, index: 1 },
{ status: 'truly', value: true, index: 2 }
]
*/
})
.catch(value => {
console.log(value);
});
cb1 = () => true;
cb2 = () => false;
cb3 = () => true;
assertAll([cb1, cb2, cb3], 3, 1000)
.then(values => {
console.log(values);
/*
[
{ status: 'truly', value: true, index: 0 },
{ status: 'falsely', value: false, index: 1 },
{ status: 'truly', value: true, index: 2 }
]
*/
});
`Main apis
These features are can use more easily and simply sometimes in certain situation
`javascript
import {
once,
delay,
every,
some,
toSync
} from 'meari';once(() => { console.log('once'); }, 1000)(); // once
delay(1000).then(() => { console.log('start'); }); // start
let p1, p2, p3;
p1 = Promise.resolve(1);
p2 = Promise.resolve(2);
p3 = Promise.resolve(3);
every([p1, p2, p3]).then(v => {
console.log(v); // true
});
p1 = Promise.reject(1);
p2 = Promise.reject(2);
p3 = Promise.resolve(3);
some([p1, p2, p3]).then(v => {
console.log(v); // true
});
p1 = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(1), 300);
});
};
p2 = () => {
return new Promise((resolve) => {
setTimeout(() => resolve(2), 100);
});
};
toSync(p1()).then(v => console.log(v));
toSync(p2()).then(v => console.log(v));
// 1
// 2
`$3
- once([callback], [delayTime]) ⇒
function
This function calls callback function only once as the async after given delayTime(created timerId will be automatically dispose after performed it)
- delay([delayTime]) ⇒
Promise
This function will delay code execution for given delayTime
- seq(promises) ⇒
Promise
This function returns an array which containing every results performed in order of a promises if haven't been rejected
- seqAll(promises) ⇒
Promise
This function returns an array which containing every results performed in order of a promises
- map(promises, callback, context) ⇒
Promise
This function returns an array which containing every results performed in order of a promises if haven't been rejected
- mapAll(promises, callback, context) ⇒
Promise
This function returns an array which containing every results performed in order of a promises
- race(promises) ⇒
Promise
This function returns an array which containing every results in order a promise performed if haven't been rejected
- raceAll(promises) ⇒
Promise
This function returns an array which containing every results in order a promise performed
- retry(callbacks, [retryCount], [delayTime]) ⇒
Promise
This function retry a callback function as much as given retryCount to until fulfilled each a promise if haven't been rejected
- retryAll(callbacks, [retryCount], [delayTime]) ⇒
Promise
This function retry a callback function as much as given retryCount to until fulfilled each a promise
- assert(callbacks, [retryCount], [delayTime]) ⇒
Promise
This function retry a callback function as much as given retryCount to until each callback function returns true if haven't been rejected
- assertAll(callbacks, [retryCount], [delayTime]) ⇒
Promise
This function retry a callback function as much as given retryCount to until each callback function returns true
- every(promises) ⇒
Promise
This function returns true when succeed every promises otherwise returns false
- some(promises) ⇒
Promise
This function returns true when succeed to promise at least one otherwise returns false
- toSync(promise) ⇒
Promise
This function performs synchronously a promise
$3
This function calls callback function only once as the async after given delayTime(created timerId will be automatically dispose after performed it)Kind: global function
Returns: function - executor
| Param | Type | Default |
| --- | --- | --- |
| [callback] | function | function(){} |
| [delayTime] | number | 0 |
Example
`js
once(() => {}, 1000)();
`
$3
This function will delay code execution for given delayTimeKind: global function
| Param | Type | Default |
| --- | --- | --- |
| [delayTime] | number | 0 |
Example
`js
delay(1000).then(() => {});
`
$3
This function returns an array which containing every results performed in order of a promises if haven't been rejectedKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
seq([Promise.resolve(), Promise.resolve()]).then(values => {}).catch(value => {});
`
$3
This function returns an array which containing every results performed in order of a promisesKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
seqAll([Promise.resolve(), Promise.resolve()]).then(values => {});
`
$3
This function returns an array which containing every results performed in order of a promises if haven't been rejectedKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
| callback | function | A function which will be call on every promises |
| context | \* | A value which will be use as context(this) when performed callback function |
Example
`js
map([Promise.resolve(), Promise.resolve()], (value) => value).then(values => {}).catch(value => {});
`
$3
This function returns an array which containing every results performed in order of a promisesKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
| callback | function | A function which will be call on every promises |
| context | \* | A value which will be use as context(this) when performed callback function |
Example
`js
mapAll([Promise.resolve(), Promise.resolve()], (value) => value).then(values => {});
`
$3
This function returns an array which containing every results in order a promise performed if haven't been rejectedKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
race([Promise.resolve(), Promise.resolve()]).then(values => {}).catch(value => {});
`
$3
This function returns an array which containing every results in order a promise performedKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
raceAll([Promise.resolve(), Promise.resolve()]).then(values => {});
`
$3
This function retry a callback function as much as given retryCount to until fulfilled each a promise if haven't been rejectedKind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| callbacks | function \| Array | | A function or array which returns the promise or any value |
| [retryCount] | number | 0 | |
| [delayTime] | number | 0 | |
Example
`js
retry([Promise.resolve(), Promise.resolve()], 3, 1000).then(values => {}).catch(value => {});
`
$3
This function retry a callback function as much as given retryCount to until fulfilled each a promiseKind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| callbacks | function \| Array | | A function or array which returns the promise or any value |
| [retryCount] | number | 0 | |
| [delayTime] | number | 0 | |
Example
`js
retryAll([Promise.resolve(), Promise.resolve()], 3, 1000).then(values => {});
`
$3
This function retry a callback function as much as given retryCount to until each callback function returns true if haven't been rejectedKind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| callbacks | function \| Array | | A function or array which returns the boolean value |
| [retryCount] | number | 0 | |
| [delayTime] | number | 0 | |
Example
`js
assert([Promise.resolve(), Promise.resolve()], 3, 1000).then(values => {}).catch(value => {});
`
$3
This function retry a callback function as much as given retryCount to until each callback function returns trueKind: global function
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| callbacks | function \| Array | | A function or array which returns the boolean value |
| [retryCount] | number | 0 | |
| [delayTime] | number | 0 | |
Example
`js
assertAll([Promise.resolve(), Promise.resolve()], 3, 1000).then(values => {});
`
$3
This function returns true when succeed every promises otherwise returns falseKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
every([Promise.resolve(), Promise.resolve()]).then(v => v);
`
$3
This function returns true when succeed to promise at least one otherwise returns falseKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promises | Array | An array which containing a promise or any value |
Example
`js
some([Promise.resolve(), Promise.reject()]).then(v => v);
`
$3
This function performs synchronously a promiseKind: global function
| Param | Type | Description |
| --- | --- | --- |
| promise | Promise | A promise which will be perform synchronously |
Example
`js
toSync(Promise.resolve(1)).then(v => console.log(v));
``