DEV Community

Discussion on: 3 most common mistakes when using Promises in JavaScript

Collapse
 
stefanovualto profile image
stefanovualto

Correct me if I am wrong but I think this piece of code:

const createdPromise = new Promise((resolve, reject) => {
  somePreviousPromise.then(result => {
    // do something with the result
    resolve(result);
  }).catch(reject);
});

Could be simplyfied by avoiding the external creation of the wrapping Promise:

const createdPromise = somePreviousPromise
  .then((result) => {
      // do something with the result
      return result;
    });
  });

Because returning the result will resolve the Promise chain, and the rejection will be forwarded.

Collapse
 
mpodlasin profile image
mpodlasin

Hey, you are absolutely correct. Wrapping promise into a promise is presented as an antipattern here. I will update an article to make that more clear!

Thanks!