Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用async.parallel处理阵列_Javascript_Arrays_Node.js_Asynchronous - Fatal编程技术网

Javascript 使用async.parallel处理阵列

Javascript 使用async.parallel处理阵列,javascript,arrays,node.js,asynchronous,Javascript,Arrays,Node.js,Asynchronous,我在Node.js中使用async模块,我陷入了从async.parallel模块中的全局数组获取值顺序的问题 function(callback) { let array = [url,url,url,url,url,url,url,url,url,url]; async.parallel([ function(cb){ //working on one url from array and then remove from array }, func

我在Node.js中使用async模块,我陷入了从async.parallel模块中的全局数组获取值顺序的问题

function(callback)
{
let array = [url,url,url,url,url,url,url,url,url,url];

async.parallel([
    function(cb){
        //working on one url from array and then remove from array
    },
    function(cb){
        //working on one url from array and then remove from array
    },
    function(cb){
        //working on one url from array and then remove from array
    },
    function(cb){
        //working on one url from array and then remove from array
    },
    function(cb){
        //working on one url from array and then remove from array
    }
],function(error,result){
    if(error)
        callback(error);
    else{
        callback(null,true);
    }   
})
}
async.parallel的每个内部函数都将调用请求模块以获取另一个html页面,并在提取其url链接后,再次将这些url链接插入全局数组


我不明白我们如何知道哪个函数使用哪个数组元素?

有两种可能的方法

一个是你的行为方式

async.parallel([
    function(cb){
        //working on one url from array and then remove from array
       arr[0] // 0th here
    },
    function(cb){
        //working on one url from array and then remove from array
          arr[1] // 1st here
    },
。 . . . 然后得到的结果是[a,b,c,d,e,f],其中a,b,c,d,e,f表示数组中的第0,第1,第2,第3,第4个元素

另一种方法是通过并行方法的对象而不是数组


这不是更合适吗?@cartant我只想使用并行,每个任务都是相互独立的。我认为你需要更清楚地了解你要做什么。并行调用中的函数数与数组中的元素数有什么关系?@cartant我只想说,将有一个url数组。然后从该url中,我们将在5个不同的并行函数中调用请求模块。我不希望一次又一次调用同一url。因为如果要从数组中删除该元素,我们不知道该函数将访问哪个元素。。。它给出了错误的结果。你在哪里尝试从数组中删除元素?我只想说,将有一个url数组。然后从该url我们将在5个不同的并行函数中调用请求模块。我不希望重复调用同一url。因为我们不知道该函数将访问哪个元素
async.parallel({
    0: function(cb){
        //working on one url from array and then remove from array
       arr[0] // 0th here
    },
    1: function(cb){
        //working on one url from array and then remove from array
          arr[1] // 1st here
    }
}, function(err, results) {
    // results is now equals to: {0: a, 1: b}
})