Skip to content

Commit d51a9f7

Browse files
committed
Docs for AsyncIterator
1 parent 7947146 commit d51a9f7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/Core__AsyncIterator.resi

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
type t<'a>
2+
3+
type value<'a> = {
4+
/**
5+
Whether there are more values to iterate on before the iterator is done.
6+
*/
7+
done: bool,
8+
/**
9+
The value of this iteration, if any.
10+
*/
11+
value: option<'a>,
12+
}
13+
14+
/**
15+
`next(asyncIterator)`
16+
17+
Returns the next value of the iterator, if any.
18+
19+
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.
20+
21+
## Examples
22+
- A simple example, getting the next value:
23+
```rescript
24+
let {done, value} = await someAsyncIterator->AsyncIterator.next
25+
```
26+
27+
- Complete example, including looping over all values:
28+
```rescript
29+
// Let's pretend we get an async iterator returning ints from somewhere.
30+
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
31+
32+
33+
let processMyAsyncIterator = async () => {
34+
// ReScript doesn't have `for ... of` loops, but it's easy to mimic using a while loop.
35+
let break = ref(false)
36+
37+
while !break.contents {
38+
// Await the next iterator value
39+
let {value, done} = await asyncIterator->AsyncIterator.next
40+
41+
// Exit the while loop if the iterator says it's done
42+
break := done
43+
44+
// This will log the (int) value of the current async iteration, if a value was returned.
45+
Console.log(value)
46+
}
47+
}
48+
```
49+
*/
50+
@send
51+
external next: t<'a> => promise<value<'a>> = "next"

0 commit comments

Comments
 (0)