How does resolve differ from reject?
Short answer
resolve(value)- signals that the operation completed successfully → the promise becomes fulfilled.reject(reason)- signals that the operation completed with an error → the promise becomes rejected.
Both move the promise from pending → to a final (settled) state,
but down different result "branches".
Example
javascript
const promise = new Promise((resolve, reject) => {
const success = Math.random() > 0.5;
if (success) {
resolve('Everything went well');
} else {
reject('An error occurred');
}
});
promise
.then(result => console.log('then:', result))
.catch(error => console.log('catch:', error))
.finally(() => console.log('finally'));Possible output:
javascript
then: Everything went well
finallyor
javascript
catch: An error occurred
finallyHow they work
| Method | What it does | Promise state | What it triggers |
|---|---|---|---|
resolve(value) | Completes the promise successfully, passes the value | fulfilled | .then(value => …) |
reject(reason) | Completes the promise with an error, passes the reason | rejected | .catch(error => …) |
After one of them is called:
- the promise is "settled",
- a further call to
resolveorrejectis ignored.
Example: resolve
javascript
const promise = new Promise((resolve) => {
setTimeout(() => {
resolve('Data received!');
}, 1000);
});
promise.then(result => console.log(result));After 1 second:
javascript
Data received!The promise moved to the fulfilled state.
Example: reject
javascript
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
reject('Network error');
}, 1000);
});
promise.catch(error => console.error(error));After 1 second:
javascript
Network errorThe promise moved to the rejected state.
Important: the state can only change once
javascript
const promise = new Promise((resolve, reject) => {
resolve('OK');
reject('Error'); // ignored
});
promise.then(console.log).catch(console.error);After the first call (resolve) the promise became fulfilled,
and the second call (reject) has no effect.
Example: resolve with another promise
If you pass another Promise to resolve(),
the current promise waits for it to settle.
javascript
const inner = new Promise((resolve) => setTimeout(() => resolve('Inner result'), 1000));
const outer = new Promise((resolve) => resolve(inner));
outer.then(console.log); // logs "Inner result" after 1 sec.This is called promise unwrapping.
Handling the results
javascript
new Promise((resolve, reject) => {
resolve('OK');
}).then(console.log); // OK
new Promise((resolve, reject) => {
reject('Error');
}).catch(console.error); // ErrorAnalogy
Think of a promise as a delivery:
resolve→ the courier delivered the package (everything succeeded)reject→ the courier called and said: "address not found" (error)
SUMMARY
| Method | State | What it passes | Handled where | Analogy |
|---|---|---|---|---|
resolve(value) | fulfilled | The successful result | .then() | Package delivered |
reject(reason) | rejected | The error / rejection reason | .catch() | Courier could not deliver |
| After the call | The promise becomes settled and cannot be changed anymore | - | - | - |
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.