0

I have this loop for download some files and it's work fine.

But the files are downloaded like "2,3,4,1,5" order and not "1,2,3,4,5".

I know how to do .each async and waterfall async but I don't know how to do for this loop.

Config.TotalFiles = 5;

for(i = 1; i <= Config.TotalFiles; i++) {
   $this.CreateJSONFile(i, function() {
     cls();
   });
}

And when downloads are done I want to call my callback, I have tried this if(id == Config.TotalFiles) but it's doesn't work because the id isn't good.

How can I done an "async" process with this loop?

Thanks

3
  • async.times ? Commented Jul 25, 2017 at 10:14
  • I have always "1,5,3,4,2"... Commented Jul 25, 2017 at 11:07
  • That depends on how fast each CreatJSONFile call does its job. They're started in the expected order always. If you want to ensure they also end in the expected order, your only choice is to do it sequentially instead of in parallel (using the …Series methods). Commented Jul 25, 2017 at 11:13

3 Answers 3

1

You can use async.whilst for this:

Config.TotalFiles = 5;
var count = 1;

//pass your maincallback that you want to call after downloading of all files is complete.

var callMe = function(mainCallback){
    async.whilst(
        function() { return count <= Config.TotalFiles; },
        function(callback){
            $this.CreateJSONFile(count, function() {
                cls();
                count++;
                callback();
            });
        }, function(){
           //This function is the final callback
           mainCallback();
        })
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry but I want just a number (81) ^^
You can check my edited answer. It will do the work.
I'm going to answer some questions in a few hour, I need 15 reputation for upvote ^^ :)
0

Yes, you need to implement async for-loop. The main idea is to call callback (like continue), when current step has been completed. You may use some helpful library as mentioned above (async.js). But you also have to understand that it will not be done in parallel (so the time to complete the downloads will be increased).

These are some nice examples to help you: https://github.com/caolan/async/blob/v1.5.2/README.md#whilst

1 Comment

Can you check my answer please? I have added more code
0

Check my screen please for understand my case.

https://i.sstatic.net/azHUA.jpg

Or this pastebin https://pastebin.com/jtANeSxq

                cls();
                for(i = 1; i <= Config.TotalFolders; i++) {
                    $this.CreateJSONFile(i, function() {
                        setTimeout(function() {
                            cls();
                        }, 200);
                    });
                }

    CreateJSONFile(id, callback) {
    console.log(chalk.hex("#607D8B")(`Creating JSON Files... [${id}/${Config.TotalFolders}]`));
    var requestOptions = {
        encoding: "binary",
        method: "GET",
        uri: `${Config.Url.Source}${Config.Url.Audio}` + `${id}/skeud.json`
    };
    request(requestOptions, function (error, response, body) {
        if (!error && response.statusCode === 200) {
            if(!fs.existsSync(Config.DownloadsPath + `${id}/skeud.json`)) {
                fs.writeFile(Config.DownloadsPath + `${id}/skeud.json`, body, function(err) {
                    if(err) {
                        return console.log(err);
                    }
                    console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("created")}`));
                });
            } else {
                console.log(chalk.hex("#FFEB3B")(`JSON File ${chalk.inverse(id)} ${chalk.hex("#FFEB3B")("skipped")}`));
            }
        }
    });
    /*if(created == Config.TotalFolders) {
        callback();
    }*/
}

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.