title: Understanding promise rejection in node.js
body: <p>I am trying to understand promises in node.js. Here is a sample code</p>
  
  <pre><code>con.queryReturnPromise("SELECT * FROM bookings WHERE driverId = " + accId + " AND bookingStatus = " + config.get('BOOKING_STATUS_ACTIVE') + " LIMIT 1")
    .catch((err) =&gt; {
  
      callback({
        "message": "Success",
        "error": true,
      });
      console.log("mysql query error");
      return Promise.reject();
    })
    .spread((rows, fields) =&gt; {
      if (rows.length != 1) {
        return Promise.reject();
      }
      mBooking = rows[0];
      var query = "INSERT INTO locations SET timeStamp = " + timeStamp + " latitude = " + lat + ", longitude = " + lng + ", bookingId = " + mBooking.id;
      return con.queryReturnPromise(query)
    })
    .catch((err) =&gt; {
      if (err)
        console.log("Adding Location error" + err);
      return Promise.reject();
    })
    .spread(() =&gt; {
      return funcs.getBooking(con, mBooking.id)
    })
    .then((booking) =&gt; {
      if (mBooking.userId.toString() in userSockets) {
        userSockets[mBooking.userId].emit(config.get('EVENT_USER_UPDATE_BOOKING'), {
          "message": "Success",
          "error": false,
          "booking": booking
        });
        userId = mBooking.userId.toString();
      }
      callback({
        "message": "Success",
        "error": false,
        "booking": booking
      });
    })
    .catch((err) =&gt; {
      if (err)
        console.log(err);
    });
  </code></pre>
  
  <p>The code is quite simple. However i have one confusion. If <code>return Promise.reject()</code> is being called, where would the function lead to, Which code will be called. E.g if the first catch clause is called and it calls return Promise.reject() where part of below code will run afterwards</p>
  
  <p>Promises in for loop</p>
  
  <pre><code>data = JSON.parse(data);
              var promisesArray = [];
              for (var i = 0; i &lt; data.length; i++) 
              {
                  var location = data[i];
                  var lng       = location.lng;
                  var lat       = location.lat;
                  var bearing   = location.bearing;
                  var deltaTime = location.deltaTime;
                  var timeStamp = location.timeStamp;
  
                  var query = "INSERT INTO locations SET timeStamp = " + timeStamp + " latitude = " + lat + ", longitude = " + lng + ", bookingId = " + mBooking.id;
  
  
                  var promise = con.queryReturnPromise(query)                    
                          .then( () =&gt; {                            
                          });
  
                  promisesArray[] = promise;
              }
  
              Promise.all(promisesArray)
              .then(function(results)
              {
                      callback({
                          "error": false,
                      });            
              });
  </code></pre>