-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFuture.resi
139 lines (116 loc) · 3.44 KB
/
Future.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
type t<'a>
let isPending: t<'a> => bool
let isCancelled: t<'a> => bool
let isResolved: t<'a> => bool
/**
* `Future.value(x)` creates a future resolved to `x`
*/
let value: 'a => t<'a>
/**
* `Future.make(initializer)` creates a future and resolves it
* with the value passed to the `resolve` function, which is passed
* as first argument to the initializer.
*
* The initializer can return an optional cancellation effect
* (e.g. a function that cancels a request or a timer).
*
* example:
*
* ```reason
* Future.make(resolve => {
* let timeoutId = Js.Global.setTimeout(resolve, 100)
* Some(() => Js.Global.clearTimeout(timeoutId))
* })
* ```
*/
let make: (('a => unit) => option<unit => unit>) => t<'a>
/**
* `Future.makePure(initializer)` creates a future and resolves it
* with the value passed to the `resolve` function, which is passed
* as first argument to the initializer.
*
* As opposed to `make`, `makePure` doesn't accept an cancellation effect.
* Only use `makePure` for side-effect free functions.
*
* example:
*
* ```reason
* Future.makePure(resolve => {
* resolve(1)
* })
* ```
*/
let makePure: (('a => unit) => unit) => t<'a>
/**
* Executes the callback when the future is resolved
*
* example:
*
* future->Future.get(Js.log)
*/
let get: (t<'a>, 'a => unit) => unit
/**
* Cancels:
* - the future you call it on
* - the futures it depends on (if the future was created using `map` or `flatMap`)
*
* Note that it doesn't cancel futures returned by the flatMap callback
*
* example:
*
* let request = getUser()
* let friends = request->Future.flatMap(getFriends)
* let transformed = request->Future.map(deserialize)
*
* request->Future.cancel // cancels `request`, `friends` and `transformed`
* friends->Future.cancel // cancels `friends` and `transformed`
* transformed->Future.cancel // cancels `transformed`
*/
let cancel: t<'a> => unit
/**
* Adds a handler to be called if the future is canceled.
*/
let onCancel: (t<'a>, unit => unit) => unit
/**
* Returns a future with the value of the source future mapped
*/
let map: (t<'a>, ~propagateCancel: bool=?, 'a => 'b) => t<'b>
/**
* Returns a future with the value of the source future mapped where the mapper returns a future itself
*/
let flatMap: (t<'a>, ~propagateCancel: bool=?, 'a => t<'b>) => t<'b>
/**
* Debug
*/
let tap: (t<'a>, 'a => unit) => t<'a>
/**
* Utils for Future.t<result<'a, 'b>>
*/
let mapResult: (
t<result<'a, 'b>>,
~propagateCancel: bool=?,
'a => result<'c, 'b>,
) => t<result<'c, 'b>>
let mapOk: (t<result<'a, 'b>>, ~propagateCancel: bool=?, 'a => 'c) => t<result<'c, 'b>>
let mapError: (t<result<'a, 'b>>, ~propagateCancel: bool=?, 'b => 'c) => t<result<'a, 'c>>
let flatMapOk: (
t<result<'a, 'b>>,
~propagateCancel: bool=?,
'a => t<result<'c, 'b>>,
) => t<result<'c, 'b>>
let flatMapError: (
t<result<'a, 'b>>,
~propagateCancel: bool=?,
'b => t<result<'a, 'c>>,
) => t<result<'a, 'c>>
let tapOk: (t<result<'a, 'b>>, 'a => unit) => t<result<'a, 'b>>
let tapError: (t<result<'a, 'b>>, 'b => unit) => t<result<'a, 'b>>
/**
* Utils for waiting for multiple futures
*/
let all2: ((t<'a>, t<'b>)) => t<('a, 'b)>
let all3: ((t<'a>, t<'b>, t<'c>)) => t<('a, 'b, 'c)>
let all4: ((t<'a>, t<'b>, t<'c>, t<'d>)) => t<('a, 'b, 'c, 'd)>
let all5: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>)) => t<('a, 'b, 'c, 'd, 'e)>
let all6: ((t<'a>, t<'b>, t<'c>, t<'d>, t<'e>, t<'f>)) => t<('a, 'b, 'c, 'd, 'e, 'f)>
let all: array<t<'a>> => t<array<'a>>