Created
January 30, 2022 05:34
-
-
Save kayac-chang/381d8dfecfc11c76628f49c49256e620 to your computer and use it in GitHub Desktop.
[learn FP with Kirby using `fp-ts`] Task
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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