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

As we know that Promise constructor takes one executor function which has two parameters which we use t

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

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

Share asked Aug 13, 2018 at 16:34 user10220492user10220492 3
  • 2 can we do like this? nope. – Jonas Wilms Commented Aug 13, 2018 at 16:40
  • You can't separate resolve and reject operations, both should be used on the same context, (resolve, reject) are callback variables and should be used in the same function in order to execute those callbacks. – Julian Torregrosa Commented Aug 13, 2018 at 16:46
  • Possible duplicate of Promise : then vs then + catch – Alexander Commented Aug 13, 2018 at 16:49
Add a ment  | 

4 Answers 4

Reset to default 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).

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.

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742293714a4416692.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信