Handling Streams Without Conflict

I'm iterating over an array of uri's that are obtained and I would like to know how I can properly control these streams while successfully writing the image files and only receive the amount of images specified in max? Somehow, this is still ignoring the count.

Outline

  1. Write the image
  2. Output the count of the downloaded image
  3. Exit if count of max is reached.

edit:

var count = 1;
var max = 1000;

images.each(function (index, element) {
  var uri = element.attribs.src;
  request(uri).pipe(fs.createWriteStream(output(uri)))
    .on('data', function () {
      console.log('[' + chalk.green('+') + ']' + ' wrote image ' + count);
      sleep.sleep(1); // pause between requests
    });
    .on('end', function () {
      count++;
      if (count === max) {
        process.exit(0);
      }
    });
    .on('error', function () {
      console.log('[' + chalk.red('x') + ']' + ' failed to write image');                                                                                                                    
    });
  });

Here's one possible solution:

var left = 1000,
    imagesChunk = images.slice(0, left);

function onDone() {
  // we're done!
}

left = imagesChunk.length;
imagesChunk.each(function (index, element) {
  var uri = element.attribs.src;
  var stream = request(uri);
  var writeStream = fs.createWriteStream(output(uri));
  var hadError = false;
  stream.on('error', function(err) {
          if (hadError)
            return;
          console.log('[' + chalk.red('✗') + ']');
          writeStream.close();
          hadError = true;
        })
        .pipe(writeStream)
          .on('error', function(err) {
            if (hadError)
              return;
            console.log('[' + chalk.red('✗') + ']');
            stream.destroy();
            hadError = true;
          })
          .on('close', function() {
            if (!hadError)
              console.log('[' + chalk.green('✓') + ']');
            if (--left === 0)
              onDone();
          });
});