You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
external make: (('a => unit, 'e => unit) => unit) => t<'a> = "Promise"
77
77
78
+
type resolvers<'a> = {
79
+
promise: t<'a>,
80
+
resolve: 'a => unit,
81
+
reject: exn => unit,
82
+
}
83
+
84
+
/**
85
+
`withResolvers()` returns a object containing a new promise with functions to resolve or reject it. See [`Promise.withResolvers`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/withResolvers) on MDN.
86
+
87
+
## Examples
88
+
89
+
```rescript
90
+
open Promise
91
+
92
+
let {promise, resolve, _} = Promise.withResolvers()
93
+
94
+
setTimeout(() => {
95
+
resolve(. "success")
96
+
}, 1000)->ignore
97
+
98
+
promise
99
+
->then(str => {
100
+
Console.log(str)->resolve
101
+
})
102
+
->ignore
103
+
```
104
+
*/
105
+
@scope("Promise")
106
+
@val
107
+
external withResolvers: unit => resolvers<_> = "withResolvers"
108
+
78
109
/**
79
110
`catch(promise, errorCallback)` registers an exception handler in a promise chain.
80
111
The `errorCallback` receives an `exn` value that can later be refined into a JS
@@ -206,7 +237,7 @@ resolve(5)
206
237
external finally: (t<'a>, unit => unit) => t<'a> = "finally"
207
238
208
239
/**
209
-
`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.
240
+
`race(arr)` runs all promises concurrently and returns promise settles with the eventual state of the first promise that settles. See [`Promise.race`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race) on MDN.
`all(promises)` runs all promises in parallel and returns a new promise resolving
237
-
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.
267
+
`any(arr)` runs all promises concurrently and returns promise fulfills when any of the input's promises fulfills, with this first fulfillment value. See [`Promise.any`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/any) on MDN.
268
+
269
+
## Examples
270
+
271
+
```rescript
272
+
open Promise
273
+
let racer = (ms, name) => {
274
+
Promise.make((resolve, _) => {
275
+
setTimeout(() => {
276
+
resolve(name)
277
+
}, ms)->ignore
278
+
})
279
+
}
280
+
281
+
let promises = [racer(1000, "Turtle"), racer(500, "Hare"), racer(100, "Eagle")]
282
+
283
+
any(promises)->then(winner => {
284
+
Console.log("The winner is " ++ winner)
285
+
resolve()
286
+
})
287
+
```
288
+
*/
289
+
@scope("Promise")
290
+
@val
291
+
external any: array<t<'a>> => t<'a> = "any"
292
+
293
+
/**
294
+
`all(promises)` runs all promises concurrently and returns a promise fulfills when all of the input's promises fulfill, with an array of the fulfillment values. See [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) on MDN.
`allSettled(promises)` runs all promises concurrently and returns promise fulfills when all of the input's promises settle with an array of objects that describe the outcome of each promise. See [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) on MDN.
356
+
357
+
```rescript
358
+
open Promise
359
+
360
+
exception TestError(string)
361
+
362
+
let promises = [resolve(1), resolve(2), reject(TestError("some rejected promise"))]
0 commit comments