0

Below is the FTP folder structure:

12341
    abc.json
    xyz.json
12342
    pqr.json
    asd.json
12343
    jkl.json
    aaa.json

Now in node script, I'm using Basic FTP to connect the FTP.

I have array like [12341,12342,12343] so I'm looping this array as below:

async.forEachOf(fileArray, async function(data, index, callback) 
{ 
    var list2 = await client.list(data+'/');

    async.forEachOf(list2, function(data, index, callback2) 
    { 
        var data = JSON.parse(JSON.stringify(data));

        latestFiles.push(data);
        
        callback2();
    },
    async function (err)
    {
        console.log("*****************************");
        console.log(latestFiles)

        callback();
    })
},
function (err) 
{
    console.log(err)
})

Above code is works fine if there is only one folder name in the array, but it is not working for multiple folders in loop.

The error is as below:

Error: User launched a task while another one is still running. Forgot to use 'await' or '.then()'?

but I'm already using await here await client.list(data+'/');

Prashant Patil
  • 2,463
  • 1
  • 15
  • 33
  • 1
    You should not use await inside forEach because it does not block the main execution thread. It only blocks the function defined inside forEach. You should use a simple 'for' loop. – Naveen Chahar Nov 24 '20 at 14:25
  • Check out this question : https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop – Naveen Chahar Nov 24 '20 at 14:25

0 Answers0