@@ -19,6 +19,86 @@ type value<'a> = {
19
19
value: option<'a>,
20
20
}
21
21
22
+ /**
23
+ `make(nextFn)`
24
+
25
+ Creates an async iterator from a function that returns the next value of the iterator.
26
+
27
+ ## Examples
28
+ - A simple example, creating an async iterator that returns 1, 2, 3:
29
+ ```rescript
30
+ let context = ref(0)
31
+
32
+ let asyncIterator = AsyncIterator.make(async () => {
33
+ let currentValue = context.contents
34
+ // Increment current value
35
+ context := currentValue + 1
36
+
37
+ {
38
+ AsyncIterator.value: Some(currentValue),
39
+ done: currentValue >= 3
40
+ }
41
+ })
42
+
43
+ // This will log 1, 2, 3
44
+ await asyncIterator->AsyncIterator.forEach(value =>
45
+ switch value {
46
+ | Some(value) => Console.log(value)
47
+ | None => ()
48
+ }
49
+ )
50
+ ```
51
+ */
52
+ let make: (unit => promise<value<'value>>) => t<'value>
53
+
54
+ /**
55
+ `value(value)`
56
+
57
+ Shorthand for creating a value object with the provided value, and the `done` property set to false.
58
+
59
+ ## Examples
60
+ ```rescript
61
+ let context = ref(0)
62
+
63
+ let asyncIterator = AsyncIterator.make(async () => {
64
+ let currentValue = context.contents
65
+ // Increment current value
66
+ context := currentValue + 1
67
+
68
+ if currentValue >= 3 {
69
+ AsyncIterator.done()
70
+ } else {
71
+ AsyncIterator.value(currentValue)
72
+ }
73
+ })
74
+ ```
75
+ */
76
+ let value: 'value => value<'value>
77
+
78
+ /**
79
+ `done(~finalValue=?)`
80
+
81
+ Shorthand for creating a value object with the `done` property set to true, and the provided value as the final value, if any.
82
+
83
+ ## Examples
84
+ ```rescript
85
+ let context = ref(0)
86
+
87
+ let asyncIterator = AsyncIterator.make(async () => {
88
+ let currentValue = context.contents
89
+ // Increment current value
90
+ context := currentValue + 1
91
+
92
+ if currentValue >= 3 {
93
+ AsyncIterator.done()
94
+ } else {
95
+ AsyncIterator.value(currentValue)
96
+ }
97
+ })
98
+ ```
99
+ */
100
+ let done: (~finalValue: 'value=?) => value<'value>
101
+
22
102
/**
23
103
`next(asyncIterator)`
24
104
@@ -30,7 +110,7 @@ See [async iterator protocols](https://developer.mozilla.org/en-US/docs/Web/Java
30
110
- A simple example, getting the next value:
31
111
```rescript
32
112
@val external asyncIterator: AsyncIterator.t<int> = "someAsyncIterator"
33
- let {AsyncIterator.done, value} = await asyncIterator->AsyncIterator.next
113
+ let value = await asyncIterator->AsyncIterator.next
34
114
```
35
115
36
116
- Complete example, including looping over all values:
0 commit comments