Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nashaofu/parallel-to-serial
Parallel task based on Promise converted to serial task
https://github.com/nashaofu/parallel-to-serial
conversion parallel promise serial
Last synced: 7 days ago
JSON representation
Parallel task based on Promise converted to serial task
- Host: GitHub
- URL: https://github.com/nashaofu/parallel-to-serial
- Owner: nashaofu
- License: mit
- Created: 2018-08-16T14:13:46.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-07-09T01:50:57.000Z (over 5 years ago)
- Last Synced: 2024-10-11T04:12:04.874Z (about 1 month ago)
- Topics: conversion, parallel, promise, serial
- Language: TypeScript
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# parallel-to-serial
Parallel task based on Promise converted to serial task
## examples
```js
const parallelToSerial = require('parallel-to-serial')function square(val) {
return new Promise((resolve, reject) => {
setTimeout(() => {
const result = Math.pow(val, 2)
console.log(`${val} -> ${result}`)
resolve(result)
}, 1000)
})
}let timer
const data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const task = data.map(val => () => {
clearTimeout(timer)
// 并发多条时不打印-----
// 保证为下一次调用切片时,在切片中间打印-----
timer = setTimeout(() => console.log('--------'))
return square(val)
})console.log('并发量为1')
parallelToSerial(task, 1)
.then(val => {
console.log('整个任务执行完成:', data, '->', val)
})
.then(() => {
console.log('\n并发量为3')
return parallelToSerial(task, 3)
})
.then(val => {
console.log('整个任务执行完成:', data, '->', val)
})
```Output results:
```
并发量为1
--------
1 -> 1
--------
2 -> 4
--------
3 -> 9
--------
4 -> 16
--------
5 -> 25
--------
6 -> 36
--------
7 -> 49
--------
8 -> 64
--------
9 -> 81
--------
10 -> 100
整个任务执行完成: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] -> [ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]并发量为3
--------
1 -> 1
2 -> 4
3 -> 9
--------
4 -> 16
5 -> 25
6 -> 36
--------
7 -> 49
8 -> 64
9 -> 81
--------
10 -> 100
整个任务执行完成: [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] -> [ 1, 4, 9, 16, 25, 36, 49, 64, 81, 100 ]
```