Created
May 22, 2016 13:06
-
-
Save rpominov/9b4e3f858b35905e66465211f783dc13 to your computer and use it in GitHub Desktop.
Task concept
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
type Result<L,R> = | |
| {T: 'Right', value: R} | |
| {T: 'Left', value: L} | |
| {T: 'Thrown', error: any} | |
| {T: 'Cancel'} | |
type Cancel = () => void | |
type RunHandler<L,R> = | |
| (result: Result<L,R>) => void | |
| { | |
Right?: (value: R) => void, | |
Left?: (value: L) => void, | |
Thrown?: (error: any) => void, | |
Cancel?: () => void, | |
} | |
type RunOptions = { | |
catch?: boolean | |
} | |
type Task<L,R> = { | |
run(handler: RunHandler<L,R>, options?: RunOptions): Cancel | |
} |
Alternative approach 1
- Instead of making it
Task<Result<L,R,E>>
internally make it justTask<A>
- Users can wrap an
Either<L,R>
intoTask<A>
by themselves - Additionally provide a
runAndCatch(onSuccess, onError)
that uses different fromrun
strategy and wraps everything totry..catch
stopping on first exception and callingonError
with it, or callingonSuccess
as normalrun(onSuccess)
if no exceptions appeared - Note: with
runAndCatch
we will not have an ability to recover, there nochainError()
. Or we could actually havechainError()
but this is probably bad idea...
In browser you would use run()
and on the server you would use runAndCatch()
all the rest of the code should be universal and dealing with Task<A>
type, using Task<Either<L,R>>
if necessary.
Alt 2
Like #1
but with Task<L,R>
. L
and R
only for user's explicit "lefts" / "rights", catched exceptions never go there. Exceptions still go only into onError
callback in runAndCatch
and can't be recovered from etc.
And yeah, we don't need onCancel
callback probably...
Alt 3
Like #2
but catched exceptions go into L
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Features
catch:false
in browser,catch:true
on server)Left