0

I want to catch error in a callback function.

(async function(){
    function wait(ms){
        return new Promise(function(resolve, reject){
            setTimeout(resolve, ms);
        });
    }

    async function test(){
        while(true) {
            if (window.err){
                throw 'This is an error!';
            }
            await wait(500);
        }
    }

    
    try {
        console.log('running ...');

        window.err = false;
        setTimeout(function(){
            window.err = true;
        }, 1000);

        test();
    
        await wait(3000);
        console.log('end')
        console.log('done')
    }
    catch(err){
        // How do i catch error of "test" function in this
        console.log('end')
        console.log('error')
    }
})()

.................................................................................................................................................................................................... How do i catch error of test function in this? My englist is not good, help me pls! Thanks!

2 Answers 2

1

test is an asynchronous function, if you call it without awaiting it the errors will be ignored.

Instead of test() use await test().

Or if you don't want to await it and just be notified when an error happens, you can use Promise.catch:

test().catch(err => {
  // Handle the error in some way
  console.error(err)
}) 
Sign up to request clarification or add additional context in comments.

3 Comments

I know it, but i want to run a function like (multi thread). It check a condition in everytime!
@nguyenphuong that's not possible. JS is single threaded. Unless you use web workers.
@nguyenphuong Can you give more details. What are you trying to achieve? I may be able to show you with generators.
0

if you want to catch the error from the test function you can try like this

(async function(){
    function wait(ms){
        return new Promise(function(resolve, reject){
            setTimeout(resolve, ms);
        });
    }

    async function test(){
      return new Promise((resolve, reject)  => {
       try {
          while(true) {
            if (window.err){
                throw 'This is an error!';
            }
            await wait(500);
            resolve();
          }
       } catch(err) {
             reject(err) // here we are catching the error and throw it back to the caller 
       }
      });
    }

    
    try {
        console.log('running ...');

        window.err = false;
        setTimeout(function(){
            window.err = true;
        }, 1000);

       try {
         await test();
       } catch (err) {
         console.log(err) // here we get the error from the test
       }

    
        await wait(3000);
        console.log('end')
        console.log('done')
    }
    catch(err){
        // How do i catch error of "test" function in this
        console.log('end')
        console.log('error')
    }
})()

I hope it helps you out

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.