0
function testPromise(id) {
    return new Promise(function(resolve, reject) {
        setTimeout(function() {
            if (typeof id === 'number') {
                resolve(id);
            } else {
                reject('error');
            }
        }, 500);
    }
    );
}
async function test(){
  try{
    testPromise(10)
    for(let i=0 ; i<10000; i++){
      ....
    }
    .
    .
  }catch(err){
    console.log(err)
  }
}
test();

ok consider above code I want to run testPromise function asynchronously so if I use await syntax testPromise(10) run first then for loop code run if, I don't use await, testPromise(10) run asynchronously and everything is good up to now, however, if testPromise encounter an error how should I handle it? is there any way to use async-await structure and handle this error inside a try-catch structure?
I know I can use catch function testPromise(10).catch(err =>{}); to handle error but I want to know is there any way to handle it inside my first level try-catch

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Daniel.V
  • 2,322
  • 7
  • 28
  • 58
  • possible duplicate of [Can I fire and forget a promise](https://stackoverflow.com/q/32384449/1048572)? – Bergi Feb 21 '18 at 21:16
  • So you don't want the loop to wait, but you want the `try` block to wait for it to finish? – Bergi Feb 21 '18 at 21:17

2 Answers2

3

You can't.

You either use try-catch along with await or you use catch on the promise.

You should take into account that the error could be thrown even when your function (test) already finished its execution.

See @Bergi's answer for a correct example.

lilezek
  • 6,976
  • 1
  • 27
  • 45
  • That might lead to an unhandled promise rejection though. Don't do that. – Bergi Feb 21 '18 at 21:14
  • If `testPromise(10)` gets rejected *and* an exception is thrown from the loop code so that the `await` is not evaluated, the rejected `promise` is dangling. – Bergi Feb 21 '18 at 23:52
  • Yes, synchronous code can throw an exception as well. – Bergi Feb 22 '18 at 08:02
  • The double try/catch appears to be quite pointless now, you'd put the outer one around the `await promise` only then. And notice that [unhandled rejections can still happen](https://stackoverflow.com/q/46889290/1048572) when the `promise` rejects while the code in the loop `await`s something else. – Bergi Feb 22 '18 at 14:45
2

If you want to run it concurrently with your loop and properly handle errors from both (which necessarily includes waiting for both), then Promise.all is your only choice:

async function test() {
  try {
    await Promise.all([
      testPromise(10),
      (async function() {
        for (let i=0; i<10000; i++) {
          …
        }
        …
      }())
    ]);
  } catch(err) {
    console.log(err);
  }
}
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • it's a good solution, however, I think it makes my code looks ugly :) – Daniel.V Feb 21 '18 at 21:25
  • 1
    @Daniel.V Best make a separate function declaration for the loop part instead of using an AIIFE. And unfortunately it's the only correct solution :-) – Bergi Feb 21 '18 at 21:26