3

I can't catch reject in my function. I have searched at google, but I haven't found solution, so please help me with this piece of code:

async function errorFunc(){
    setTimeout(() => {
        return Promise.reject("Any error occured!");
    }, 1000);
}
async function main(){
    await errorFunc().catch((error)=>{
        console.log("E in catch:", error);
    });
    
    try{
        await errorFunc();
    }catch(e){
        console.log("E in try-catch:", e);
    }
}

main();

No one of that catch (in main() function) works...In console, there is simply printed (twice) this error message:

Uncaught (in promise) Any error occured!

I want to catch that error (or better say promise rejection). How can I do that in my main() function?

Thanks!

3
  • 1
    Your errorFunc is not returning the Promise you reject, so it cannot be caught. Something like this should work return new Promise((resolve, reject) => setTimeout(() => reject('Any error occurred!'), 1000)); Commented Jan 24, 2021 at 11:48
  • You are right, now it works. But - is there way to not return Promise? Why I dont want to return it? Because it is another "wrapper" which code makes less readable... I thought that when I declare function as "async", it automatically means it is Promise... So I can simply call Promise.reject() inside of it and it will behave just like normal promise...Is there way to do what I want? (use Promise.reject() without returning promise). Commented Jan 24, 2021 at 11:55
  • 1
    Yes, async makes it return a Promise. But if you don't use await anywhere in that function, that Promise is fullfilled immediatly (resolved, in this case). Whatever you do in the Timeout is no longer related to that Promise. Explicitly returning a "home-made" promise is needed if you want to keep a handle to it in your Timeout, so you can resolve it or reject it Commented Jan 24, 2021 at 11:56

3 Answers 3

3
async function errorFunc(){
    return new Promise((resolve, reject) => setTimeout(() => reject("Any error occured!"), 1000))
}

this is what you want.


setTimeout(callback, ms)

callback will be exec in global, no one catch it.

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

Comments

1

I think it printed the same error twice, because in the second function:

try{
        await errorFunc();
    }catch(e){
        console.log("E in try-catch:", error);
    }
}

you printed "error" but caught "e", I didn't understand the issue that much but I suppose that that's the reason it printed twice,

maybe swap "error" with "e" to print the other error caught

3 Comments

This + what's described in 王仁宏's answer are needed
You are right, I have error there, I should print "e" and not "error", but it is not what I am searching for...
Well I hope that helped, As for the other question, I didn't mention it as I didn't know the answer but I thought I should mention it because you said it printed twice
0

You must return a Promise from your function

async function errorFunc(){
   return new Promise((resolve,reject)=>{
   setTimeout(() => {
      return reject("Any error occured!");
   }, 1000);
  })
}

async function main(){
   await errorFunc().catch((error)=>{
      console.log("E in catch:", error);
   });

   try{
      await errorFunc();
   }catch(e){
      console.log("E in try-catch:", e);
   }
}

Comments

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.