Monday, 3 April 2017

Promise.all().then() goes to catch() because then is not recognized

The code is running well until the Promise.all and then is goes right to the catch saying 'then is not defined'.

I've been trying to figure this out without success for hours :(.

Any help are welcome.

Here is a simplified example of code:

// Save
return new Promise((fulfillSave, rejectSave) => {
  // Get
  this._getObjects().then((object) => {
    var promises = [];

    // For each value
    object.values.forEach((value) => {
        promises.push(
          // Create promise
          new Promise((fulfill, reject) => {
            // Create MDB object + assign value
            valueMongoDB.value = value; 
            // Save
            valueMongoDB.save((err, results) => {
              if (err) {
                reject('Error in saving');
              } else {
                fulfill();
              }
            });
          })
        );
      });

      // Wait for all promises
      Promise.all(promises).then(() => {
        // Nothing to do
        fulfillSave();
      }, then((err) => {
        // Err
        rejectSave(err);
      }));
    }
  }).catch((err) => {
    rejectSave(`Error: ${err.message}`);
  });
});

Thanks in advance! Serge.



via Serge

No comments:

Post a Comment