简体   繁体   中英

What is the difference between Promise((resolve,reject)=>{}) and Promise(resolve =>{})?

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 !!!

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 ).

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM