Skip to content

Instantly share code, notes, and snippets.

Last active Jun 5, 2021
What would you like to do?
RemoteData for TypeScript. Read more about how to use this pattern here:
type RemoteData<E, D> =
| { type: "NOT_ASKED" }
| { type: "LOADING" }
| { type: "FAILURE"; error: E }
| { type: "SUCCESS"; data: D };
function foldRemoteData<R, E, D>(
remoteData: RemoteData<E, D>,
notAsked: () => R,
loading: () => R,
failure: (error: E) => R,
success: (data: D) => R
): R {
switch (remoteData.type) {
case "NOT_ASKED":
return notAsked();
case "LOADING":
return loading();
case "FAILURE":
return failure(remoteData.error);
case "SUCCESS":
return success(;
Copy link

praveeno commented Jun 5, 2021

npm i -S gist:2def3e32daa41e5dbc6b9664bf001429

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment