Borislav Hadzhiev
Tue Oct 19 2021·1 min read
Photo by Vadim Sadovski
The "Promise.reject is not a constructor" error occurs when we try to use the
Promise.reject()
method with the new
operator. The Promise.reject()
method
is not a constructor so it should be used without the new
operator, e.g.
Promise.reject('err')
.
Here's an example of how the error is caused.
// ⛔️ Promise.reject is not a constructor const err = new Promise.reject(new Error('boom'));
Instead, we should not use the new operator with the Promise.reject method.
// ✅ works const rejected = Promise.reject(new Error('boom'));
Promise.reject is a method, not a constructor. The only parameter the method takes is the reason why the promise should be rejected.
The Promise.reject()
method returns a promise that is rejected with the
provided reason.
Promise.reject
method, Promise()
is a constructor which is used to wrap functions that don't already support promises.The following 2 examples achieve the same result:
const r1 = Promise.reject(new Error('boom')); const r2 = new Promise((resolve, reject) => { reject(new Error('boom')); });
Both of the examples return a promise that is rejected with the provided reason,
however the Promise.reject
method provides us a more direct and concise
solution than the
Promise()
constructor for this use case.