News for hackers. Stuff that matters.
Re:Code optimizations when using async/await by fathyb on Tuesday January 04, 2022 @11:00AM (#29796216)
Using a try/catch block won't attach an error handler at the right time (before the next event loop tick):

    // promise1 will resolve after one second
    let promise1 = new Promise(resolve => setTimeout(resolve, 1000))
    // promise2 will wait for the next even loop tick to call its error handler
    let promise2 = Promise.reject('something bad')

    // At this point, you should await promise2, or set an error handler using .catch
    // But you can do some sync stuff if you want to:
    someExpensiveCPUStuff()

    // But if you do anything async, it'll print a warning in Node.js
    let result1 = await promise1
If you ever need to store a promise and its error over time (ie. an async cache), you can wrap the value, and unwrap it when needed:

    let wrapped = promise
      .then(result => ({ type: 'resolved', result }))
      .catch(error => ({ type: 'rejected', error }))

    await doSomeAsyncStuff()
    
    let result = await wrapped.then(data => {
      if(data.type === 'resolved') {
        return data.result
      } else {
        throw data.error
      }
    })
Re:Code optimizations when using async/await by mfbx9da4 on Tuesday January 04, 2022 @11:24AM (#29796551)
> will wait for the next even loop tick to call its error handler

are you sure about that?

Promise.reject is synchronous and won't wait for the next tick it just returns a rejected promise

try/catch can be used fine

    async function f() {
      throw new Error("asdf");
    }

    let result1 = f();
    let result2 = f();

    try {
      await result1;
    } catch (e) {
      console.log("caught the error");
    }

    try {
      await result2;
    } catch (e) {
      console.log("caught the error");
    }
Re:Code optimizations when using async/await by fathyb on Tuesday January 04, 2022 @11:52AM (#29796875)
The error handler won't be called until the next tick:

    Promise
      .reject('error')
      .catch(error => console.log('Error handler called:', error))
    console.log('After reject')
This will print:

    After reject
    Error handler called: error
Re:Code optimizations when using async/await by lhorie on Tuesday January 04, 2022 @11:59AM (#29796963)
I think the point is that try/catch in async code is tied to the presence of await. This will run happily without falling into the catch clause:

    const err = async () => {
      throw new Error('async error')
    }
    try {
      const rejected = Promise.reject();
      const errored = err();
      console.log('ok!'); // in real code, this might have returned a deferred to be handled elsewhere instead
    } catch (e) {
      console.log('not ok!'); // never gets here
    }
Re:Code optimizations when using async/await by petilon on Tuesday January 04, 2022 @12:50PM (#29797601)
I don't get a warning for this code:

    async function foo() {
        try {
            // promise1 will resolve after one second
            let promise1 = new Promise(resolve => setTimeout(resolve, 1000))
            // promise2 will wait for the next even loop tick to call its error handler
            let promise2 = Promise.reject('something bad')

            // At this point, you should await promise2, or set an error handler using .catch
            // But you can do some sync stuff if you want to:
            someExpensiveCPUStuff()

            // No warning in Node.js
            let result2 = await promise2;
            let result1 = await promise1;
        }
        catch (ex) {
            console.log(ex)
        }
    }
Re:Code optimizations when using async/await by fathyb on Tuesday January 04, 2022 @01:44PM (#29798370)
You should if you await promise1 first:

    let result1 = await promise1;
    let result2 = await promise2;
Re:Code optimizations when using async/await by petilon on Tuesday January 04, 2022 @02:09PM (#29798696)
Yeah, but the goal is to kick off two async calls in parallel. The order in which you await is unimportant for that goal. If awaiting promise2 first solves the problem then that's the solution!
Re:Code optimizations when using async/await by WolfOliver on Tuesday January 04, 2022 @03:37PM (#29800089)
so console.log(ex) is never executed?
Re:Code optimizations when using async/await by WolfOliver on Tuesday January 04, 2022 @03:50PM (#29800269)
you have to execute it like this:

await foo();

Then you will see the error