Understanding Promise.resolve() and Promise.reject()
In this tutorial, we are going to understand two very important JavaScript methods that help us convert any value into a Promise:
Promise.resolve()Promise.reject()
These methods are extremely useful when working with existing code that expects Promises, but sometimes you only have normal (non-promise) values.
Why Do We Need This?
In real-world JavaScript applications, especially when working with async programming, you often write functions that expect a Promise as input.
Let’s imagine we already have a function like this:
Example: A function that expects a Promise
function logToConsole(promise) {
promise.then(result => {
console.log(result);
});
}
This function:
- Accepts a promise
- Waits for it to resolve
- Prints the resolved value to the console
Using a Promise with This Function
Now let’s create a simple promise:
const promise = new Promise((resolve) => {
resolve("Hello");
});
When we pass this promise to our function:
logToConsole(promise);
Output:
Hello
Everything works perfectly because the function receives a valid promise.
The Problem: What If We Pass a Normal Value?
Now imagine we want to pass a normal value, like a string:
logToConsole("Hello");
What happens?
You will get an error like this:
TypeError: promise.then is not a function
Why does this error occur?
Because:
- The function expects an object that has a
.then()method. - A normal string does not have
.then(). - Therefore, JavaScript throws an error.
The Goal
We want to:
- Use the same function
- Accept both promise values and non-promise values
- Avoid writing separate logic
This is exactly where Promise.resolve() helps.
Using Promise.resolve()
What does Promise.resolve() do?
Promise.resolve(value):
- Converts any value into a resolved Promise
- If the value is already a promise, it simply returns it
- If the value is not a promise, it wraps it inside a resolved promise
Example: Converting a non-promise value
const value = "Hello"; const promise = Promise.resolve(value); logToConsole(promise);
Output:
Hello
Now everything works correctly because:
"Hello"is converted into a promise- The function receives exactly what it expects
Key takeaway:
Promise.resolve() allows you to treat normal values and promises the same way.
Creating a Rejected Promise using Promise.reject()
Just like we can create a resolved promise, we can also create a rejected promise.
Example:
const rejectedPromise = Promise.reject("Something went wrong");
This creates a promise that is immediately rejected.
Handling the rejected promise
rejectedPromise.catch(error => {
console.log(error);
});
Output:
Something went wrong
Best Practice: Use Error Objects for Rejection
Although you can reject a promise with any value, modern JavaScript best practices recommend using an Error object.
Recommended approach:
Promise.reject(new Error("Something went wrong"));
Why is this better?
- Provides stack traces
- Makes debugging easier
- Aligns with standard error-handling patterns
