Adeel Imran
Adeel Imran

Reputation: 13956

Javascript Catch In Then Promises

I am trying to figure this out. I have a promise like this

 function Function1 () {
   return fetch()
   .then((xx) => )
   .catch(error => throw(error));
}

Use this Function1 promise in another file.

Function1()
.then((xx) => ()
.catch((error) => {
  console.log('I want to Catch that stupid error here');
});

Why can't I get the error message thrown from the Function1 promise in the catch error where I am calling this Function1() ?

Any of your kind help and comments will be highly appreciated, Gracious :)

Upvotes: 1

Views: 87

Answers (1)

loelsonk
loelsonk

Reputation: 3598

Use throw inside .then function.

// Here is Promise then throw example

new Promise((resolve, reject) => {
  resolve(5);
}).then(result => {
  throw 'Err';
})
.catch(error => { 
  console.log(error); 
  throw error;
});

Upvotes: 3

Related Questions