在async.parallel中已经调用了calback并行处理?

时间:2017-01-12 15:18:10

标签: javascript node.js asynchronous parallel-processing promise

我正在使用如下的async.parallel,但我一直在调用“已经调用了回调函数。”

在browserperf下面是第三方api,它返回当前url的性能指标,并从刚刚返回url数组的json文件中读取config.data1.urls

有关browser-perf的更多信息,请访问: https://github.com/axemclion/browser-perf

async.parallel([

        function(callback) {
            Promise.mapSeries(config.data1.urls, function(url) { 
                var  totalTime= 0, endTime= 0;
                console.log(url);
                new Promise(function(resolve, reject){
                    browserPerf(url, function(error, data) {
                        console.log("browserPerf for ",url);

                        if (!error) {
                            resolve(data);
                            data = data[0];

                            let loadTime = (data.loadEventEnd - data.navigationStart)/1000 + ' sec';
                            let firstPaint = data.firstPaint;
                            let domInteractive = (data.domInteractive - data.navigationStart)/1000 + ' sec';

                            var perfData = {
                                'URL' : url,
                                'firstPaint' : firstPaint,
                                'loadTime' : loadTime,
                                'domInteractive' : domInteractive
                            };

                            self.dats.push(perfData);
                            console.log(self.dats);

                            endTime = Date.now();
                            totalTime += (endTime - self.startTime);
                            var ms = totalTime,
                            min = (ms/1000/60) << 0,
                            sec = (ms/1000) % 60;

                            console.log("Total time for data to be recorded: ", min + ':' + sec);

                            callback(null, self.dats);    


                          } else {
                            console.log(error);
                            reject(error);
                        }
                    }, {
                        selenium: 'http://localhost:4444/wd/hub',
                        browsers: ['chrome']
                    });
                });

            });                 
        },

        function(callback) {
            Promise.mapSeries(config.data2.urls, function(url) { 
                var  totalTime= 0, endTime= 0;
                console.log(url);
                new Promise(function(resolve, reject){
                    browserPerf(url, function(error, data) {
                        console.log("browserPerf for ",url);

                        if (!error) {
                            resolve(data);
                            data = data[0];

                            let loadTime = (data.loadEventEnd - data.navigationStart)/1000 + ' sec';
                            let firstPaint = data.firstPaint;
                            let domInteractive = (data.domInteractive - data.navigationStart)/1000 + ' sec';

                            var perfData = {
                                'URL' : url,
                                'firstPaint' : firstPaint,
                                'loadTime' : loadTime,
                                'domInteractive' : domInteractive
                            };

                            self.dats.push(perfData);
                            console.log(self.dats);  

                            endTime = Date.now();
                            totalTime += (endTime - self.startTime);
                            var ms = totalTime,
                            min = (ms/1000/60) << 0,
                            sec = (ms/1000) % 60;

                            console.log("Total time for data to be recorded: ", min + ':' + sec);  
                            callback(null, self.dats);             
                          } else {
                            console.log(error);
                            reject(error);
                        }

                    }, {
                        selenium: 'http://localhost:4444/wd/hub',
                        browsers: ['chrome']
                    });
                });

            });

        }
    ],function(err,result) {
            console.log(result);
    });

知道我做错了什么吗?我只想将存储在self.dats中的组合结果捕获到回调函数的结果中。

0 个答案:

没有答案