promise-serial

2019-12-04 admin

promise-serial是什么

什么是promise-serial,Run promises in series

promise-serial使用教程帮助文档

Usage

// example.js
const Promise_serial = require('promise-serial');

const promises =
    Array(15).fill()
    .map((_, i) =>
        () => new Promise(resolve => {
            console.log('promise '+i+' start');
            setTimeout(
                () => {
                    console.log('promise '+i+' end');
                    resolve('output-'+i);
                },
                500
            );
        })
    );

console.log('### Run promises in sequence')
Promise_serial(promises)
.then(() => {
    console.log('### Run promises in series of 5')
    return Promise_serial(promises, {parallelize: 5});
})
.then(output => {
    console.log('### Run promises in series of 10')
    return (
        Promise_serial(promises, {parallelize: 10})
        .then(output => {console.log('Resolved values: '+JSON.stringify(output))})
    );
});

node example.js prints:

### Run all promises in sequence
promise 0 start
promise 0 end
promise 1 start
promise 1 end
promise 2 start
promise 2 end
promise 3 start
promise 3 end
promise 4 start
promise 4 end
promise 5 start
promise 5 end
promise 6 start
promise 6 end
promise 7 start
promise 7 end
promise 8 start
promise 8 end
promise 9 start
promise 9 end
promise 10 start
promise 10 end
promise 11 start
promise 11 end
promise 12 start
promise 12 end
promise 13 start
promise 13 end
promise 14 start
promise 14 end
### Run all promises in series of 5
promise 0 start
promise 1 start
promise 2 start
promise 3 start
promise 4 start
promise 0 end
promise 1 end
promise 2 end
promise 3 end
promise 4 end
promise 5 start
promise 6 start
promise 7 start
promise 8 start
promise 9 start
promise 5 end
promise 6 end
promise 7 end
promise 8 end
promise 9 end
promise 10 start
promise 11 start
promise 12 start
promise 13 start
promise 14 start
promise 10 end
promise 11 end
promise 12 end
promise 13 end
promise 14 end
### Run all promises in series of 10
promise 0 start
promise 1 start
promise 2 start
promise 3 start
promise 4 start
promise 5 start
promise 6 start
promise 7 start
promise 8 start
promise 9 start
promise 0 end
promise 1 end
promise 2 end
promise 3 end
promise 4 end
promise 5 end
promise 6 end
promise 7 end
promise 8 end
promise 9 end
promise 10 start
promise 11 start
promise 12 start
promise 13 start
promise 14 start
promise 10 end
promise 11 end
promise 12 end
promise 13 end
promise 14 end
Resolved values: ["output-0","output-1","output-2","output-3","output-4","output-5","output-6","output-7","output-8","output-9","output-10","output-11","output-12","output-13","output-14"]

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-80424.html

文章标题:promise-serial

回到顶部