Felix
Felix

Reputation: 3134

Determine when async execution is finished

I have to process an array of entries that requires to perform to async tasks for each file entry: getFile and uploadResult both are async task. My question is how can I know when the array doc.entries is being processed using an async library like asyncjs. The code below is just an illustration of what I am trying to accomplish.

var doc = {
    version: '1.7',
    entries: [{
        date: '11/11/10',
        files: [{
            name: 100,
            executable: false
        },
        {
            name: 101,
            executable: false
        }]
    },
    {
        date: '11/12/10',
        files: [{
            name: 200,
            executable: false
        },
        {
            name: 201,
            executable: false
        }]
    },
    {
        date: '11/13/10',
        files: [{
            name: 300,
            executable: false
        }]
    },
    {
        date: '11/14/10',
        files: [{
            name: 400,
            executable: false
        }]
    }]
};

doc.entries.map(function(entry){

    entry.files.map(function(file){

        getFile(file, function(err, result){
            if(err){
                throw Error(err)
            }

            uploadResult(result, function(err, status){
                WriteOnDb(file.name, status, function(err, result){ ... });
            });
        })
    });
});

How can I know when the last file is being store on the db and then do something else?

Thanks.

Upvotes: 0

Views: 55

Answers (1)

Bogdan Savluk
Bogdan Savluk

Reputation: 6312

The easiest way is to use promises, or better observables, but you do it with callbacks too - for example you can count how many tasks are in total and how many was finished:

var total = doc.entries
  .map(function (entry) {
    return entry.files.length;
  })
  .reduce(function (x, acc) {
    return acc + x
  }, 0);

var finished = 0;

function finishCallback(err) {
  if (err === null) {
    /// all async tasks are finished;
  }
}

doc.entries.map(function (entry) {
  entry.files.map(function (file) {
    getFile(file, function (err, result) {
      if (err) {
        finishCallback(err);
      } else {
        uploadResult(result, function (err, status) {
          WriteOnDb(file.name, status, function (err, result) {
            if (err) {
              finishCallback(err);
            } else {
              finished += 1;
              if (finished === total) finishCallback(null);
            }
          });
        });
      }
    })
  });
});

Upvotes: 1

Related Questions