4

As we know that Promise constructor takes one executor function which has two parameters which we use to generate success case or failure case. Today I was programming and I was stuck but later I solve the issue but I found one thing that needs to be understood.

What is the difference between

new Promise(resolve => {

    // resolve

});

and

new Promise((resolve,reject)=>{

    // resolve
    // reject

});

Can we do like this?

new Promise(resolve => {

    // resolve

}, reject => {

    // reject

});

Examples will be more appreciated. Thanks !!!

4 Answers4

5

This is not specific to Promises, just to callback functions.

new Promise((resolve) => {});1 creates a Promise whose callback only takes the resolve parameter. It’s not possible to call the reject function that would otherwise be provided.2

new Promise((resolve, reject) => {}); creates a Promise whose callback takes both parameters, including the one for rejecting.

The above two examples demonstrate how positional parameters work. The first parameter in the callback function is always the resolve function, the second one is always the reject function.

new Promise((reject, resolve) => {}); will create a Promise in which you can resolve with reject and reject with resolve.

You could throw in the scope of the callback function or resolve(Promise.reject()) to cause a rejection to happen:

new Promise((resolve) => {
  throw new Error("42");
  // or `resolve(Promise.reject(new Error("42")));`
})
  .catch(console.warn); // Prints warning “Error: "42"” in the console.

You cannot use new Promise((resolve) => {}, (reject) => {});, since the Promise constructor only takes one argument. The second callback function will just be ignored.


1: (resolve) => {} is, of course, equivalent to resolve => {}. But arrow function parameters actually always require parentheses. Simple and single parameters are the sole exception where they can be omitted. See the MDN article about arrow function syntax.

2: Using a regular function, new Promise(function(resolve){}); or new Promise(function(){}); you could access any argument with arguments[0] (resolve) or arguments[1] (reject).

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
  • *1b) someone `throws` inside the callback, or `resolve(Promise.reject())`* – Jonas Wilms Aug 13 '18 at 16:48
  • and whats about if i do like this **new Promise(resolve,reject => {})**. Is that correct ? –  Aug 13 '18 at 16:52
  • @DhuBytes you have to wrap 2 coma separated arguments with **(** and **)** in case you have multiple arguments, eg **(resolve, reject) => {}** – Koushik Chatterjee Aug 13 '18 at 16:54
  • @DhuBytes No. Arrow function parameters always need to be wrapped in parentheses, except if there’s only one parameter. `new Promise(resolve, reject => {})` is a Promise that takes the existing variable `resolve` as the executor and will ignore the function `(reject) => {}`. – Sebastian Simon Aug 13 '18 at 16:56
  • So it means when we do something like that **abc => {}** then it means that abc is the function argument. And if we need multiple arguments we must have to do like that **(abc,def,...) => {}**. So in case of one argument parenthesis can be omitted but in case of multiple arguments, parenthesis are required. Right ? –  Aug 13 '18 at 16:59
  • @DhuBytes That is right, but it goes further than that. Parameters could also be rest parameters or destructured parameters like `(...array) => {}`, `([{value: renamed} = object]) => {}`. These are referred to as “non-simple parameters” and they all require parentheses. – Sebastian Simon Aug 13 '18 at 17:04
2

You can omit reject if you know for a fact that the promise can never fail, for example a timer. Anything that requires an error handler (http requests, file i/o, etc.) will need a reject callback.

TLP
  • 1,262
  • 1
  • 8
  • 20
  • 1
    Ok so if we pass one function then it will be considered as resolve function in the promise constructor? –  Aug 13 '18 at 16:40
  • Yes, the first parameter of the function that you pass into the `Promise` constructor is the resolve handler. The second parameter (the reject handler) is optional. – TLP Aug 13 '18 at 16:43
  • and what about if i do like this new **Promise(resolve,reject => {})**. Is that correct ? –  Aug 13 '18 at 16:52
  • You need some parenthesis around `resolve,reject` but otherwise good (assuming you'll put some code between `{}` of course ). So: `new Promise((resolve,reject) => { resolve(getSomeValue()); })` – TLP Aug 13 '18 at 16:55
  • So it means when we do something like that **abc => {}** then it means that abc is the function argument. And if we need multiple arguments we must have to do like that **(abc,def,...) => {}**. So in case of one argument parenthesis can be omitted but in case of multiple arguments, parenthesis are required. Right ? –  Aug 13 '18 at 16:59
0

The arrow function you pass is the callback triggered when the async operation ends. It accepts 2 arguments, function to be called on success,(resolve) and function to be called in case of fail (reject).

In JS you don't have to pass all params to function callback. If you don't plan to handle error (you should handle!), you can omit it.

If you pass 1 param, it is considered a resolve fn.

justMe
  • 674
  • 7
  • 26
0

Well, forget about promises, if any function having one argument get called with two arguments there is no problem as par JavaScript standard (and vise versa).

Now, related to your promise, the callback you are passing to your constructor will get called by 2 arguments (the resolver function and the rejector). If you create function having 1 argument and pass that to constructor of Promise, simply it will get called by 2 arguments, since you have no reference to the second argument you cannot use that in however manner it supposed to be user (as a generic statement and not for promise).

You can still try using arguments if you still need the second argument but, with arrow function you will not get that too. In that case it's better to use normal function () {}. Otherwise you can try returning another promise explicitly with Promise.resolve or Promise.reject

And definately the last one with multiple callback as arguments to Promise constructor is not going to work, because that are designed like having one callback with 2 parameter.

Koushik Chatterjee
  • 4,106
  • 3
  • 18
  • 32