Skip to content

Instantly share code, notes, and snippets.

@kayac-chang
Created January 30, 2022 05:34
Show Gist options
  • Save kayac-chang/381d8dfecfc11c76628f49c49256e620 to your computer and use it in GitHub Desktop.
Save kayac-chang/381d8dfecfc11c76628f49c49256e620 to your computer and use it in GitHub Desktop.
[learn FP with Kirby using `fp-ts`] Task
import { describe } from "../utils";
/**
* Task
* ===
* A task is a function that returns a promise which is expected to never be rejected.
*/
/**
* Why use Task?
*/
const asyncFunction = async () => Promise.resolve();
describe(`
when you have an operation that can fail,
but is handled by reducing both the success and failure outcomes into a single type.
`, () => {
const boolTask: () => Promise<boolean> = async () => {
try {
await asyncFunction();
return true;
} catch (err) {
return false;
}
};
});
import { Task } from "fp-ts/lib/Task";
describe(`Use Task implement`, () => {
const boolTask: Task<boolean> = async () => {
try {
await asyncFunction();
return true;
} catch (err) {
return false;
}
};
});
/**
* Constructor
*/
import * as T from "fp-ts/lib/Task";
describe("Task Constructor", () => {
const foo = "asdf";
const bar = T.of(foo);
// Same as
const baz: Task<string> = () => Promise.resolve("abc");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment