| Feature | fp-ts (Task/TaskEither) | Effect |
|---|---|---|
| Async primitive | Task<A> = () => Promise<A> (lazy) |
Effect<Out, Err, Req> (runnable via runPromise, etc.) |
| Error channel | TaskEither<E, A> = () => Promise<Either<E, A>> |
Dedicated error channel in Effect<Out, Err, Req> |
| Exception wrapping | tryCatch wraps thrown/rejected Promises into Left<E> |
Effect.try / Effect.tryPromise wrap sync/async functions |
| Composition | pipe, chain, map, fold on TaskEither |
.pipe(Effect.map, Effect.chain, Effect.catchAll, …) |
| Concurrency | No built-in fibers; manual via Promise.all |
First-c |
This file contains hidden or 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
| const stream = (value, next = null) => ({ | |
| map: function(f) { | |
| return stream(f(value), next ? next.map(f) : null) | |
| }, | |
| filter: function(pred) { | |
| if (pred(value)) { | |
| return stream(value, next ? next.filter(pred) : null) | |
| } else { | |
| return next ? next.filter(pred) : emptyStream |
This file contains hidden or 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
| export function cancellable<A>( | |
| p: Promise<A>, | |
| ok: (a: A) => void, | |
| err: (e: Error) => void, | |
| fin?: () => void | |
| ) { | |
| let cancelled = false; | |
| p.then((v) => cancelled || ok(v)) | |
| .catch((e) => cancelled || err(e)) | |
| .finally(() => cancelled || fin?.()); |
This file contains hidden or 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
| var deepDiffMapper = function () { | |
| return { | |
| VALUE_CREATED: 'created', | |
| VALUE_UPDATED: 'updated', | |
| VALUE_DELETED: 'deleted', | |
| VALUE_UNCHANGED: 'unchanged', | |
| map: function(obj1, obj2) { | |
| if (this.isFunction(obj1) || this.isFunction(obj2)) { | |
| throw 'Invalid argument. Function given, object expected.'; | |
| } |
This file contains hidden or 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
| var Id = v => ({v: v,map: f => Id(f(v)) }); | |
| var CoFreeF = (a, next) => ({ | |
| a: a, | |
| next: next, | |
| extract: () => a, | |
| map: g => CoFreeF(g(a), () => next().map(x=>x.map(g))), | |
| }); | |
| var f = n => CoFreeF(n + 1, () => Id(f(n + 1))); |
This file contains hidden or 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
| var Id = v => ({v: v,map: f => Id(f(v)) }); | |
| var CoFreeF = (a, next) => ({ | |
| a: a, | |
| next: next, | |
| extract: () => a, | |
| map: g => CoFreeF(g(a), () => next().map(x=>x.map(g))), | |
| }); | |
This file contains hidden or 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
| interface DynamicPromiseResult { | |
| data: unknown; | |
| promise: string; | |
| } | |
| const promises: Array<Promise<DynamicPromiseResult>> = [] | |
| if (true) { // some logic | |
| const promise1 = fetch('https://jsonplaceholder.typicode.com/posts/1').then(data => ({ data, promise: 'promise1' })); | |
| promises.push(promise1); | |
| } |
This file contains hidden or 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
| const movieLists = [ | |
| // ... | |
| ]; | |
| const boxartSizeRestrictions = boxart => | |
| boxart.width === 150 && boxart.height === 200; | |
| const videoBoxartMapper = (boxart, video) => ({ | |
| id: video.id, | |
| title: video.title, |
This file contains hidden or 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
| var movieLists = [ | |
| { | |
| name: "Instant Queue", | |
| videos: [ | |
| { | |
| id: 70111470, | |
| title: "Die Hard", | |
| boxarts: [ | |
| { | |
| width: 150, |
This file contains hidden or 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
| var movieLists = [{ | |
| name: "Instant Queue", | |
| videos: [{ | |
| "id": 70111470, | |
| "title": "Die Hard", | |
| "boxarts": [{ | |
| width: 150, | |
| height: 200, | |
| url: "http://cdn-0.nflximg.com/images/2891/DieHard150.jpg" | |
| }, |
NewerOlder