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

Type definition for what for await takes #36153

Open
5 tasks done
lukiano opened this issue Jan 12, 2020 · 3 comments
Open
5 tasks done

Type definition for what for await takes #36153

lukiano opened this issue Jan 12, 2020 · 3 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@lukiano
Copy link

lukiano commented Jan 12, 2020

I haven't found this request on the current issues so I'm opening one.

Search Terms

"for await" asynchronous generators

Suggestion

If for await can take both an Iterable and an AsyncIterable then I'd like a type alias

type ForAwaitable<T> = Iterable<T> | AsyncIterable<T>;

// or maybe ForAwaitable<T, U = T> = Iterable<T> | AsyncIterable<U>;

Better names are welcome.

Use Cases

type ForAwaitable<T> = Iterable<T> | AsyncIterable<T>;

async function foo(chunks: ForAwaitable<Buffer>): Promise<void> {
  for await (const chunk of chunks) {
    // ...
  }
}

await foo(someReadableStream);
await foo(inMemoryBufferArray);

Looking at https://tc39.es/proposal-async-iteration/#sec-createasyncfromsynciterator and after a test, if an object has both fields Symbol.asyncIterator and Symbol.iterator then the former takes precedence, but that use case should rarely happen.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@Jamesernator
Copy link

Having used the type quite a lot I'd prefer to see it called something like AsyncOrSyncIterable<T> rather than ForAwaitable as it's not necessarily the case you'll consume it with a for-await-of loop rather than just accessing iterator methods directly.

@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript labels Jan 13, 2020
@Vandivier
Copy link

Vandivier commented Apr 4, 2020

I prefer ForAwaitable for these reasons:

  1. It's more concise.
  2. AsyncOrSyncIterable is logically and semantically reduced to Iterable, but this is an incorrect deduction. Therefore, the name is counterintuitive.
  3. ForAwaitable doesn't mean one must use it with for await, but rather that it is supported to do so. This exactly preserves the intent of the alias.
  4. ForAwaitable trends with Awaitable which is currently under discussion Add a new type Awaitable<T> = T | PromiseLike<T> #31394

@jimblackler
Copy link

+1, I have this precise type definition locally in my code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants