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

The tuple leads to a unsound type, it also breaks the already existing allSettled pattern #27

Closed
gabrielricardourbina opened this issue Aug 21, 2024 · 4 comments

Comments

@gabrielricardourbina
Copy link

gabrielricardourbina commented Aug 21, 2024

Using a tuple as a result of this operator is a mistake.

JS allows promises to be rejected with any type, it also allows promises to be resolved with any type

The tuple cannot guarantee that is possible to know if the given promise failed or passed

For example

const [result, error] ?= await Promise.reject(undefined)
const [result, error] ?= await Promise.resolve(undefined)

Without knowing the implementation there is no guaranteed way of knowing if the promise failed or passed,

This has already been solved by Promise.allSettled

This new syntax must be syntactic sugar for this helper function

async function settled(promise) {
  const [settled] = await Promise.allSettled([promise])
  return settled
}

Addendum

As per #27 (comment), the structure of the resulting object might need to be different from the PromiseSettledResult for synchronous functions, although I don't mind using the same type

@gabrielricardourbina
Copy link
Author

See #3 (comment)

@arthurfiorette
Copy link
Owner

The allSettled pattern is just for promises, status: 'fulfilled', and status: 'rejected' is as well.

@gabrielricardourbina
Copy link
Author

@arthurfiorette You're right, I'm overlooking sync functions, in any case it has to be a discriminable object.

Personally I don't mind using the same type as Promise.allSettled for sync functions

@arthurfiorette
Copy link
Owner

It also doesn't have to be an object, falsy throwns are catched by the implementation too, i just didn't added it to the written spec yet because i wanted to see other solutions.

https://github.com/arthurfiorette/tuple-it/blob/32dc86108ae76e5c118bedf320c574c6ac8d6e41/index.js#L13

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