https://github.com/exoticknight/serialpromise
run promises with timeout, in series
https://github.com/exoticknight/serialpromise
Last synced: 3 months ago
JSON representation
run promises with timeout, in series
- Host: GitHub
- URL: https://github.com/exoticknight/serialpromise
- Owner: exoticknight
- License: mit
- Created: 2018-07-12T16:27:33.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2023-05-05T11:54:51.000Z (about 2 years ago)
- Last Synced: 2025-02-21T13:47:37.760Z (4 months ago)
- Language: TypeScript
- Size: 37.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SerialPromise
run promises with timeout, in queue
> you can alse check [parallel-promise](https://www.npmjs.com/package/parallel-promise)
## Install
```bash
npm install queue-promise --save
```## Usage
```javascript
import SerialPromise, { StatusCode } from 'serial-promise'SerialPromise(
[
lastResult => {
// lastResult equals null at first
// return a promise that resolves in 100ms
},
lastResult => {
// lastResult equals { status: StatusCode.RESOLVE, data:[your data] }
// return a promise that resolves in 3000ms
},
lastResult => {
// lastResult equals { status: StatusCode.TIMEOUT, timeout: 2000 }
// return a promise that rejects in 100ms
},
lastResult => {
// lastResult equals { status: StatusCode.REJECT, err:[error] }
// return a promise that rejects in 3000ms
},
],
[1000, 2000, 1000, 2000], // timeout for each promise, no timeout if undefined
500, // time delay between each promise, you can even use [500, 1000, 3000] to control
(progress) => {
// called whenever a promise's state is changed
// progress == {
// total: 4, total count of promise
// resolve: [], array of the index of the resolved promises
// reject: [], array of the index of the rejected promises
// timeout: [], array of the index of the timeouted promise
// }
}
).then(rets => {
// rets == [
// { status: StatusCode.RESOLVE, data:[your data] },
// { status: StatusCode.TIMEOUT, timeout: 2000 }
// { status: StatusCode.REJECT, err:[error] },
// { status: StatusCode.TIMEOUT, timeout: 2000 }
// ]
})// NOTE that there is not .catch here
```You can use async/await as well.
## StatusCode
| Status | StatusCode |
|---|---|
| RESOLVE | 0 |
| REJECT | 1 |
| TIMEOUT | -1 |## Test
```bash
npm run test
```## License
MIT