Friday, 17 March 2017

Propagating promise error in the middle function

I want to await for a function returning a promise and handle an error here in one place, this is exactly what I do:

try {
  await doSomething();
} catch (e) {
  console.log(e);
}

var doSomething = () => {
  return getPromise();
};

var getPromise = () => {
  return new Promise((resolve, reject) => {
    if (2 == 2) ? resolve() : reject();
  });
};

The thing is that I'm getting an unhandled promise exception here. I have to handle an error both in the doSomething function and in the await part of code. Is there a way to avoid this, and handle the error in one place only?



via user99999

No comments:

Post a Comment