Skip to content

Commit e267806

Browse files
authored
Docs for Promise (#41)
* format promise docstrings * some fixes
1 parent b1cc1af commit e267806

File tree

1 file changed

+79
-20
lines changed

1 file changed

+79
-20
lines changed

src/Core__Promise.resi

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,19 @@
66
//
77
// More details about polymorphism / invariance / covariance,... can be found here:
88
// https://caml.inria.fr/pub/docs/manual-ocaml/polymorphism.html#ss:variance:abstract-data-types
9+
10+
/***
11+
Functions for interacting with JavaScript Promise.
12+
See: [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).
13+
*/
14+
915
type t<+'a> = promise<'a>
1016

1117
/**
12-
[resolve(value)] creates a resolved Promise with a given `value`
18+
`resolve(value)` creates a resolved Promise with a given `value`.
19+
See [`Promise.resolve`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve) on MDN.
20+
21+
## Examples
1322
1423
```rescript
1524
let p = Promise.resolve(5) // promise<int>
@@ -19,13 +28,27 @@ let p = Promise.resolve(5) // promise<int>
1928
@scope("Promise")
2029
external resolve: 'a => t<'a> = "resolve"
2130

31+
/**
32+
`reject(exn)` reject a Promise.
33+
See [`Promise.reject`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject) on MDN.
34+
35+
## Examples
36+
37+
```rescript
38+
exception TestError(string)
39+
40+
let p = Promise.reject(TestError("some rejected value"))
41+
```
42+
*/
2243
@scope("Promise") @val
2344
external reject: exn => t<_> = "reject"
2445

2546
/**
2647
`make(callback)` creates a new Promise based on a `callback` that receives two
2748
uncurried functions `resolve` and `reject` for defining the Promise's result.
2849
50+
## Examples
51+
2952
```rescript
3053
open Promise
3154
@@ -53,8 +76,11 @@ external make: ((@uncurry (. 'a) => unit, (. 'e) => unit) => unit) => t<'a> = "P
5376

5477
/**
5578
`catch(promise, errorCallback)` registers an exception handler in a promise chain.
56-
The `errorCallback` receives an `exn` value that can later be refined into a JS error or ReScript
57-
error. The `errorCallback` needs to return a promise with the same type as the consumed promise.
79+
The `errorCallback` receives an `exn` value that can later be refined into a JS
80+
error or ReScript error. The `errorCallback` needs to return a promise with the
81+
same type as the consumed promise. See [`Promise.catch`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch) on MDN.
82+
83+
## Examples
5884
5985
```rescript
6086
open Promise
@@ -87,15 +113,17 @@ reject(SomeError("this is an error"))
87113
->ignore // Ignore needed for side-effects
88114
```
89115
90-
In case you want to return another promise in your `callback`, consider using \`then\` instead.
116+
In case you want to return another promise in your `callback`, consider using
117+
`then` instead.
91118
*/
92119
let catch: (t<'a>, exn => t<'a>) => t<'a>
93120

94121
/**
95-
`then(promise, callback)` returns a new promise based on the result of `promise`'s value.
96-
The `callback` needs to explicitly return a new promise via `resolve`.
97-
122+
`then(promise, callback)` returns a new promise based on the result of `promise`'s
123+
value. The `callback` needs to explicitly return a new promise via `resolve`.
98124
It is **not allowed** to resolve a nested promise (like `resolve(resolve(1))`).
125+
See [`Promise.then`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) on MDN.
126+
## Examples
99127
100128
```rescript
101129
Promise.resolve(5)
@@ -113,9 +141,11 @@ Promise.resolve(5)
113141
external then: (t<'a>, @uncurry ('a => t<'b>)) => t<'b> = "then"
114142

115143
/**
116-
`thenResolve(promise, callback)` converts an encapsulated value of a promise into another promise wrapped value.
144+
`thenResolve(promise, callback)` converts an encapsulated value of a promise
145+
into another promise wrapped value. It is **not allowed** to return a promise
146+
within the provided callback (e.g. `thenResolve(value => resolve(value))`).
117147
118-
It is **not allowed** to return a promise within the provided callback (e.g. `thenResolve(value => resolve(value))`).
148+
## Examples
119149
120150
```rescript
121151
resolve("Anna")
@@ -128,14 +158,18 @@ resolve("Anna")
128158
->ignore // Ignore needed for side-effects
129159
```
130160
131-
In case you want to return another promise in your `callback`, consider using \`then\` instead.
161+
In case you want to return another promise in your `callback`, consider using
162+
`then` instead.
132163
*/
133164
@send
134165
external thenResolve: (t<'a>, @uncurry ('a => 'b)) => t<'b> = "then"
135166

136167
/**
137-
[finally(promise, callback)] is used to execute a function that is called no matter if a promise
138-
was resolved or rejected. It will return the same `promise` it originally received.
168+
`finally(promise, callback)` is used to execute a function that is called no
169+
matter if a promise was resolved or rejected. It will return the same `promise`
170+
it originally received. See [`Promise.finally`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/finally) on MDN.
171+
172+
## Examples
139173
140174
```rescript
141175
exception SomeError(string)
@@ -167,12 +201,35 @@ resolve(5)
167201
@send
168202
external finally: (t<'a>, unit => unit) => t<'a> = "finally"
169203

170-
/* Combining promises. */
204+
/**
205+
`race(arr)` combining `array` of promises. See [`Promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) on MDN.
206+
207+
## Examples
208+
209+
```rescript
210+
open Promise
211+
let racer = (ms, name) => {
212+
Promise.make((resolve, _) => {
213+
Global.setTimeout(() => {
214+
resolve(. name)
215+
}, ms)->ignore
216+
})
217+
}
218+
219+
let promises = [racer(1000, "Turtle"), racer(500, "Hare"), racer(100, "Eagle")]
220+
221+
race(promises)->then(winner => {
222+
Console.log("The winner is " ++ winner)
223+
resolve()
224+
})
225+
```
226+
*/
171227
@scope("Promise") @val
172228
external race: array<t<'a>> => t<'a> = "race"
173229

174230
/**
175-
[all(promises)] runs all promises in parallel and returns a new promise resolving all gathered results in a unified array.
231+
`all(promises)` runs all promises in parallel and returns a new promise resolving
232+
all gathered results in a unified array. See [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) on MDN.
176233
177234
```rescript
178235
open Promise
@@ -194,44 +251,46 @@ all(promises)
194251
external all: array<t<'a>> => t<array<'a>> = "all"
195252

196253
/**
197-
[all2((p1, p2))]. Like `all()`, but with a fixed size tuple of 2
254+
`all2((p1, p2))`. Like `all()`, but with a fixed size tuple of 2
198255
*/
199256
@scope("Promise")
200257
@val
201258
external all2: ((t<'a>, t<'b>)) => t<('a, 'b)> = "all"
202259

203260
/**
204-
[all3((p1, p2, p3))]. Like `all()`, but with a fixed size tuple of 3
261+
`all3((p1, p2, p3))`. Like `all()`, but with a fixed size tuple of 3
205262
*/
206263
@scope("Promise")
207264
@val
208265
external all3: ((t<'a>, t<'b>, t<'c>)) => t<('a, 'b, 'c)> = "all"
209266

210267
/**
211-
[all4((p1, p2, p3, p4))]. Like `all()`, but with a fixed size tuple of 4
268+
`all4((p1, p2, p3, p4))`. Like `all()`, but with a fixed size tuple of 4
212269
*/
213270
@scope("Promise")
214271
@val
215272
external all4: ((t<'a>, t<'b>, t<'c>, t<'d>)) => t<('a, 'b, 'c, 'd)> = "all"
216273

217274
/**
218-
[all5((p1, p2, p3, p4, p5))]. Like `all()`, but with a fixed size tuple of 5
275+
`all5((p1, p2, p3, p4, p5))`. Like `all()`, but with a fixed size tuple of 5
219276
*/
220277
@scope("Promise")
221278
@val
222279
external all5: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>)) => t<('a, 'b, 'c, 'd, 'e)> = "all"
223280

224281
/**
225-
[all6((p1, p2, p4, p5, p6))]. Like `all()`, but with a fixed size tuple of 6
282+
`all6((p1, p2, p4, p5, p6))`. Like `all()`, but with a fixed size tuple of 6
226283
")*/
227284
@scope("Promise")
228285
@val
229286
external all6: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>, t<'f>)) => t<('a, 'b, 'c, 'd, 'e, 'f)> = "all"
230287

231288
/**
232-
`done` is a safe way to ignore a promise. If a value is anything else than a promise, it will raise a type error.
289+
`done(p)` is a safe way to ignore a promise. If a value is anything else than a
290+
promise, it will raise a type error.
233291
*/
234292
external done: promise<'a> => unit = "%ignore"
235293

294+
// TODO: add docstring
236295
external unsafe_async: 'a => promise<'a> = "%identity"
237296
external unsafe_await: promise<'a> => 'a = "?await"

0 commit comments

Comments
 (0)