How to safely write multiple files at once?

Tags: , ,



I’m trying to write multiple audio files from URL at once in node.js.

Here’s a code that seems to work, but I’m not sure if this is a safe way to do it since there are many asynchronous callbacks in a for loop.

var fs = require('fs');
var http = require('http');

function writeAudioFilesFromUrls(urls) {
  var len = urls.length;
  var files = Array(len).fill();
  var count = 0;
  for (let i = 0; i < len; i++) {
    files[i] = fs.createWriteStream(`output/audio${i}.mp3`);
    http.get(urls[i], function (res) {
      res.pipe(files[i]);
      files[i].on('finish', function () {
        files[i].close(function () {
          if (++count == len) {
            console.log("All files are sucessfully written!");
          }
        });
      });
    }); 
  }
}
writeAudioFilesFromUrls(["http://s5.qhres.com/static/465f1f953f1e6ff2.mp3", "http://s5.qhres.com/static/465f1f953f1e6ff2.mp3"]);

Is this safe to use this code? If not, how can I fix it?

ADDED: Or is it possible to just make the function work synchronously?

Answer

It is safe, you generally want to worry when you attempt to write multiple streams (or whatever) to the same file.

You could use fs.writeFileSync to get peace of mind but that will most likely slow down your program and block your process which may be something you don’t want to do depending on your use case.



Source: stackoverflow