1

Why doesn't it run the code "console.log(err)" below? but instead returns "TypeError: Cannot create property 'uncaught' on string 'error in promise'"

function abc() {
    throw "error in promise";
    return 123;
};
abc().catch(function(err) {
    console.log(err)
}).then ( abcMessage =>
    console.log(abcMessage)
)
3
  • 1
    Don’t throw strings; throw errors. new Error("error in promise") Commented Apr 14, 2017 at 1:54
  • thanks! amended it to new Error("error in promise") and the error is gone but now instead of running console.log(err) it throws the error Error: error in promise before it reaches console.log(err) Commented Apr 14, 2017 at 1:58
  • 1
    where's the promise? Commented Apr 14, 2017 at 1:59

1 Answer 1

2

.then and .catch require a Promise to be constructed. You are not returning a promise. The promise callback (executor) takes two arguments, a resolver, and a rejecter. Depending on what happens in the code, you may need to call resolve if everything goes right, or reject if something goes wrong.

function abc() {
  return new Promise(function(resolve, reject) {
    reject(123)
  });
};


abc()
  .catch(err => {
    console.log(err);
    return err;
  })
  .then(abcMessage => {
    console.log(abcMessage)
  });
new Error("error in promise")

Sign up to request clarification or add additional context in comments.

1 Comment

What you call the "promise callback" actually has an official name, which is executor.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.