-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement asyncPipe #643
Merged
Merged
Implement asyncPipe #643
Changes from 8 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe596ab
implement async pipe
kennylavender b83561f
-implement async pipe
kennylavender 5521743
Merge branch 'master' into async-pipe
kennylavender b7da776
fix tests
kennylavender b8854d7
Update async-pipe.test.ts
kennylavender fab2df3
Update async-pipe.test.ts
kennylavender 81b7f85
remove try, it does not help because it does not await
kennylavender 2eb3ed5
Merge branch 'async-pipe' of github.com:poetapp/node into async-pipe
kennylavender abf939d
Merge branch 'master' into async-pipe
kennylavender 3906b96
Merge branch 'master' into async-pipe
kennylavender File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { identity } from 'ramda' | ||
import { describe } from 'riteway' | ||
import { asyncPipe } from './async-pipe' | ||
import { toPromise } from './to-promise' | ||
|
||
const double = (x: number) => x * 2 | ||
const inc = (x: number) => x + 1 | ||
|
||
const doubleP = toPromise(double) | ||
const incP = toPromise(inc) | ||
|
||
describe('asyncPipe', async (assert: any) => { | ||
const should = 'apply the value to the functions composition correctly' | ||
|
||
{ | ||
const value = 10 | ||
assert({ | ||
given: 'sync and async functions', | ||
should, | ||
actual: await asyncPipe(doubleP, inc)(value).catch(identity), | ||
expected: inc(await doubleP(value)), | ||
}) | ||
} | ||
|
||
{ | ||
const value = 10 | ||
assert({ | ||
given: 'nested compositions of sync and async functions', | ||
should, | ||
actual: await asyncPipe(doubleP, inc, asyncPipe(double, incP))(value).catch(identity), | ||
expected: await incP(double(inc(await doubleP(value)))), | ||
}) | ||
} | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
type Func = (v?: any) => any | Promise<any> | ||
|
||
type asyncPipe = (...fns: Func[]) => (v?: any) => Promise<any> | ||
export const asyncPipe: asyncPipe = (...fns) => v => fns.reduce(async (a, c) => c(await a), v) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sadly TypeScript doesn't have any decent support for the the most important FP functions like curry, compose, pipe... and even some cases of reduce. See this comment by one of the authors of Ramda's type declarations, and the Variadict Kinds Proposal.
You can hack around TS' limitation by providing one function overload per function arity.
It isn't pretty or very maintainable, but I think it still would add a lot of value since we rarely compose/pipe too many functions and it'd give us type safety in our pipes.
Wanna give it a shot?
Feel free to merge as-is though, code looks good and the tests are awesome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, for some reason I though they had solved it partial in TS 3.0, but when I started looking around it seems they have not.
I think I will merge this as is, then I will go back and add the better typings.