title: Saving multiple files using Promise
body: <p>I have a Node URL (created using Express) that can be used to download a static image of an address. So, the calling app calls the /download url and passes multiple addresses using json, then the download service would call Google Maps  and save the static image of each of these addreses on the node server (it would then send it back to the calling application, but for the purpose of this question I am only interested in saving the images in the node server).</p>
  
  <p>Initially, we were only interested in saving satellite view of the addresses, so I wrote this code</p>
  
  <pre><code>var request = require('request');
  function saveMap(addressJson) {
      return Promise.all(
          //iterate over address json, get each address, call the Google Maps URL and save it.
          addressJson.map(function(item) {
               return new Promise(function (resolve, reject) {
                   var mapUrl = 'http://maps.googleapis.com/maps/api/staticmap?maptype=roadmap&amp;markers=size:mid|color:red|' + item.address;
                   request(mapUrl)
                       .pipe(fs.createWriteStream('/temp/' + item.id + '.jpg'))
                       .on('finish', function () {
                           resolve("Promise resolved");
                       }).on('error', function (error) {
                           reject('Error in creating map', error);
                       })
               });
          })
      )
  }
  </code></pre>
  
  <p>The Promise for saving each address is wrapped inside Promise.all(..), since I want to the saveMap() to return when all maps have finished downloading (so that I can zip them and send to the calling app, so need to be sure that everything has been downloaded).</p>
  
  <p>Now, we need to extend this function to also include satellite maps. I was hoping, that within same json iteration, I can have another Promise which would download satellite maps. Something like this </p>
  
  <pre><code>function saveMap(addressJson) {
      return Promise.all(
          //iterate over address json, get each address, call the Google Maps URL and save it.
          addressJson.map(function(item) {
               return new Promise(function (resolve, reject) {
                   var mapUrl = 'http://maps.googleapis.com/maps/api/staticmap?maptype=roadmap|' + item.address;
                   request(mapUrl)
                       .pipe(fs.createWriteStream('/temp/r/' + item.id + '.jpg'))
                       .on('finish', function () {
                           resolve("Promise resolved");
                       }).on('error', function (error) {
                           reject('Error in creating map', error);
                       })
               });
               return new Promise(function (resolve, reject) {
                   var mapUrl2 = 'http://maps.googleapis.com/maps/api/staticmap?maptype=satellite|' + item.address;
                   req(mapUrl2)
                       .pipe(fs.createWriteStream(fs.createWriteStream('/temp/s/' + item.id + '.jpg'))
                       .on('finish', function () {
                           resolve("Promised resolved");
                       }).on('error', function (error) {
                           reject('Error in creating map', error);
                       })
               });
          })
      )
  }
  </code></pre>
  
  <p>However, this does not work as expected. I don't get an error but it is not generating all the jpg files. Can someone please help me understand what I am doing wrong and how to get this corrected.</p>