码迷,mamicode.com
首页 > 其他好文 > 详细

Promise的串行,并行,并发

时间:2020-05-10 21:31:22      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:ati   round   code   ble   time   push   res   ini   catch   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>promise test</title>
</head>
<body>
    <script>
         Promise.serial = function(promiseGens,allowFail){
            let seq = Promise.resolve();
            promiseGens.forEach(function(item){
                seq = allowFail ? seq.then(item).catch(err => err) : seq.then(item);
            });

            return seq;
        }

        //单个promise reject后,继续执行
        Promise.parallel = function(promiseGens){
            let allPromise = new Promise(function(resolve,reject){
                let results = [];
                promiseGens.forEach(function(item){
                    item()
                    .then(data => results.push(data))
                    .catch(err => results.push(err))
                    .finally(function(){
                        if(results.length == promiseGens.length){
                            resolve(results);
                        }
                    })
                });
            });

            return allPromise;
        }

        Promise.concurrency = function(promiseGens,concurrency,allowFail){
            let promiseGenCopys = [].concat(promiseGens);
            let concurrencyPromises = [];
            let res = [];
            while(concurrency--){
                concurrencyPromises.push(recur(promiseGenCopys));
            }
            
            return Promise.all(concurrencyPromises).then(_ => res);

            // return Promise.parallel(concurrencyPromises);

            function recur(promiseGens){
                if(!promiseGens.length) return Promise.resolve();

                let first = promiseGens.shift();
                return first().then(function(data){
                    res.push(data);
                    return recur(promiseGens);
                }).catch(function(err){
                    res.push(err);
                    return allowFail ? recur(promiseGens) : err; 
                })
            }
        }
    </script>
    <script>
        // 异步函数a
        var a = function () {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(resolve a);
                    resolve(a);
                }, 500)
            })
        }

        // 异步函数b
        var b = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(() => {  
                    console.log(reject b,data);
                    reject(data||reject+ b);
                },500)
            })
        }

        // 异步函数c
        var c = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(resolve c,data);
                    resolve(data||reject + c);
                }, 500)
            })
        }

        // 异步函数c
        var d = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(resolve d,data);
                    resolve(data||reject + d);
                }, 500)
            })
        }

        // 异步函数c
        var e = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(reject e,data);
                    reject(data||reject + e);
                }, 500)
            })
        }

        // 异步函数c
        var f = function (data) {
            return new Promise(function (resolve, reject) {
                setTimeout(function () {
                    console.log(reject f,data);
                    reject(data||reject + f);
                }, 500)
            })
        }

        //串行测试
        // Promise.serial([a,b,c,d,e,f],true).then(function(data){
        //     console.log(data);
        // }).catch(function(err){
        //     console.log(‘catch‘,err);
        // });

        //并行测试
        // Promise.parallel([a,b,c,d,e,f]).then(function(data){
        //     console.log(data);
        // }).catch(function(err){
        //     console.log(err);
        // });

        //并发测试
        Promise.concurrency([a,b,c,d,e,f],2,true).then(function(data){
            console.log(data);
        }).catch(function(err){
            console.log(err);
        })
    </script>
</body>
</html>

 

Promise的串行,并行,并发

标签:ati   round   code   ble   time   push   res   ini   catch   

原文地址:https://www.cnblogs.com/mengff/p/12864797.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!