Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More complex async function example #20

Open
zaverden opened this issue Aug 19, 2024 · 1 comment
Open

More complex async function example #20

zaverden opened this issue Aug 19, 2024 · 1 comment

Comments

@zaverden
Copy link

Let say I have this function:

function unstableTimeout(n) {
  if (n < 1) throw new Error1()

  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (n < 10) reject(new Error2())
      resolve("done")
    }, n)
  })
}

from polyfill section:

const [error, data] ?= await unstableTimeout(arg1, arg2)
// should become
const [error, data] = await unstableTimeout[Symbol.result](arg1, arg2)

So unstableTimeout[Symbol.result] will return me [Error1, null] or [null, promise] - tuple that is not awaitable.

I just don't understand how it should work. For const [err, data] = unstableTimeout(n), I expect to get in err - Error1, Error2 or null, in data - "done" or null. But provided polyfill does not do this.

Could you please explain more on this matter?

@zaygraveyard
Copy link

zaygraveyard commented Aug 21, 2024

As noted in "Recursive Handling" of the README, results are recursively handled so unstableTimeout[Symbol.result] will return [Error1, null | undefined], [Error2, null | undefined], or [null, "done"] (as you expect).

The polyfill matches this behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants