@@ -127,8 +127,8 @@ export const getOrElse = <L, A>(f: Lazy<A>) => (ma: RemoteData<L, A>): A => (isS
127
127
* It applies a function to each case in the data structure.
128
128
*
129
129
* @example
130
- * const onInitial = "it's initial"
131
- * const onPending = "it's pending"
130
+ * const onInitial = () => "it's initial"
131
+ * const onPending = () => "it's pending"
132
132
* const onFailure = (err) => "it's failure"
133
133
* const onSuccess = (data) => `${data + 1}`
134
134
* const f = fold(onInitial, onPending, onFailure, onSuccess)
@@ -160,6 +160,28 @@ export const fold = <E, A, B>(
160
160
}
161
161
} ;
162
162
163
+ /**
164
+ * A more concise way to "unwrap" values from {@link RemoteData} "container".
165
+ * It uses fold in its implementation, collapsing `onInitial` and `onPending` on the `onNone` handler.
166
+ * When fold's `onInitial` returns, `onNode` is called with `none`.
167
+ *
168
+ * @example
169
+ * const onNone = (progressOption) => "no data to show"
170
+ * const onFailure = (err) => "sorry, the request failed"
171
+ * const onSuccess = (data) => `result is: ${data + 1}`
172
+ * const f = fold(onInitial, onPending, onFailure, onSuccess)
173
+ *
174
+ * f(initial) // "no data to show"
175
+ * f(pending) // "no data to show"
176
+ * f(failure(new Error('error text'))) // "sorry, the request failed"
177
+ * f(success(21)) // "result is: 22"
178
+ */
179
+ export const fold3 = < E , A , R > (
180
+ onNone : ( progress : Option < RemoteProgress > ) => R ,
181
+ onFailure : ( e : E ) => R ,
182
+ onSuccess : ( a : A ) => R ,
183
+ ) : ( ( fa : RemoteData < E , A > ) => R ) => fold ( ( ) => onNone ( none ) , onNone , onFailure , onSuccess ) ;
184
+
163
185
/**
164
186
* One more way to fold (unwrap) value from {@link RemoteData}.
165
187
* `Left` part will return `null`.
0 commit comments