crazyones110
crazyones110

Reputation: 397

What does resolve and reject actually do in Javascript Promise

At first, I think resolve simply pass the parameter to the function in then, so I tried this

const promise = new Promise((resolve, reject) => {
  resolve(new Promise(resolve => resolve(2333)))
})
// promise.then(innerPromise => {
//   innerPromise.then(num => console.log(num))
// })
promise.then(num => console.log(num))

The lines commented got an error:innerPromise.then is not a function, so I assume resolve will firstly help you handle the promise inside if you have a promise as the paramter

So I tried reject, I think it will be the same

const promise = new Promise((resolve, reject) => {
  reject(new Promise(resolve => resolve(2333)))
})
promise.then(null, innerPromise => {
  innerPromise.then(num => console.log(num))
})
// promise.then(null, num => console.log(num))

The lines uncommented will log 2333, the lines commented will simply log the rejected Promise instance

Upvotes: 3

Views: 3822

Answers (2)

xdeepakv
xdeepakv

Reputation: 8125

By the defination, resolve can take value and thenable value. If thenable value, it will resolve and return the state. However, reject only take as value. Even if value it promise(thenable). It wont resolve it.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve

Argument to be resolved by this Promise. Can also be a Promise or a thenable to resolve

The Promise.resolve() method returns a Promise object that is resolved with a given value. If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value. This function flattens nested layers of promise-like objects (e.g. a promise that resolves to a promise that resolves to something) into a single layer.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject

The Promise.reject() method returns a Promise object that is rejected with a given reason.

Upvotes: 0

Imtiyaz Shaikh
Imtiyaz Shaikh

Reputation: 625

Resolve is a callback used to return the value or the result of another promise.

So when you execute the below code in first block:

const promise = new Promise((resolve, reject) => {
  resolve(new Promise(resolve => resolve(2333)))
})

promise.then(innerPromise => {
      console.log(innerPromise)
//    innerPromise.then(num => console.log(num))
})

promise.then(num => console.log(num))

You will find the inner functions are executed automatically by the Resolve, thus getting the final result of functions inside resolve. Secondly, it is clearly mentioned that 'Resolve' returns a 'Value' and not a callable object. So, you cannot call the result of a promise like a function.

In case of Reject, reject callback is used to reject the promise with a provided reason or error. You may return any statement (that represents error) or a callable object that returns you the error message from error code. (That's how I use the Reject to fetch the messages from Error codes generated)

Well, you can read more here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Upvotes: 1

Related Questions