Skip to content

Docs for AsyncIterator #33

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

Merged
merged 2 commits into from
Feb 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions src/Core__AsyncIterator.resi
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/***
Bindings to async iterators, a way to do async iteration in JavaScript.

See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.*/

/**
The type representing an async iterator.
*/
type t<'a>

type value<'a> = {
/**
Whether there are more values to iterate on before the iterator is done.
*/
done: bool,
/**
The value of this iteration, if any.
*/
value: option<'a>,
}

/**
`next(asyncIterator)`

Returns the next value of the iterator, if any.

See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols) on MDN.

## Examples
- A simple example, getting the next value:
```rescript
let {done, value} = await someAsyncIterator->AsyncIterator.next
```

- Complete example, including looping over all values:
```rescript
// Let's pretend we get an async iterator returning ints from somewhere.
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"


let processMyAsyncIterator = async () => {
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
let break = ref(false)

while !break.contents {
// Await the next iterator value
let {value, done} = await asyncIterator->AsyncIterator.next

// Exit the while loop if the iterator says it's done
break := done

// This will log the (int) value of the current async iteration, if a value was returned.
Console.log(value)
}
}
```
*/
@send
external next: t<'a> => promise<value<'a>> = "next"