简体   繁体   中英

Multiple Promise.reject with es6

I m actually coding some functions using chained promises and I m having some trouble dealing with them.

Actually, here's the anti-pattern that I want to avoid :

  return promise.then((res)=>{
    return promise2.then((result)=>{
      return result;
    },(error)=>{
      return Promise.reject(error);
    });
  },(err)=>{
    return Promise.reject(err);
  });

I tried refactoring it the following :

  return promise.then((res)=>{
    return promise2;
  },(err)=>{
    return Promise.reject(err);
  }).then((result)=>{
    return result;
  },(error)=>{
    return Promise.reject(error);
  });

The fact is that I don't know at all of my rejected promises will act. It seems that my unit tests are all breaking after this refactor and I don't understand why. Maybe the behaviour...

In fact, does my (error) callback contain the (err) rejected value, or will it break completely and return the promise directly ?

Thanks for your help.

EDIT : okay I ll try with another example . Let's imagine that I need to make some arithmetic opeartions asynchronously by catching each possible error, at each possible level

  asyncAddition(5, -5).then((res)=>{
    return asyncDivide(100, res);
  },(err)=>{
    return Promise.reject('ERROR, you cant divide by 0');
  }).then((res)=>{
    return asyncMultiply(res, 10);
  }).then((res)=>{
    return res;
  },(err)=>{
    return Promise.reject('An error occured, number result is over 1000 ');
  });

Does the first promise rejected will break all the other in the chain ? Or will it continue ?

An (error)=>{return Promise.reject(error);} error callback is about as useless as a (result)=>{return result;} success callback. Used with then , it's an identity. You can simply omit them:

return promise.then(res => promise2);

It seems that my unit tests are all breaking after this refactor and I don't understand why.

The two snippets you are posted will have equivalent results. Their exact flow might be a bit different, eg Promise.reject being called twice in the second example when promise is rejected, but if both promise and promise2 are standard promises with Promises/A+ semantics then this must not matter.

okay I ll try with another example . Let's imagine that I need to make some operations asynchronously by catching each possible error, at each possible level

Yes, it does make quite a difference here. Catching errors on multiple levels, and treating them distinctly, will have different outcomes. An error handler will handle all errors on a promise, that is, in the complete chain above it (but not in the scuccess callback of the same then call, see difference between .then(…).catch(…) and .then(…, …) for details).

I believe you are actually looking for

asyncAddition(5, -5).then(res => {
    return asyncDivide(100, res).catch(err =>{
        throw new Error('ERROR, you cant divide by 0');
    });
}).then(res => {
    return asyncMultiply(res, 10).catch(err => {
        throw new Error('An error occured, number result is over 1000');
    });
});

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