Passing context to computations when calling run()
Not sure whether this is a good idea or not, but it might be...
The idea is to make this possible:
const task = Task.create((onSucc, onFail, context) => {
// context === 123
// ...
}).chain(x => Task.create((onSucc, onFail, context) => {
// context === 123
// ...
})
task.run(onSucc, onFail, 123)
This might be useful for testing for example. We could pass an API for data fetching as context, and during testing we could use a mocked fetching API.
It'd be useful not just for testing.
I want to run some computation multiple times with different parameters. How should I do that?
Yeah, this is certainly not only for testing, testing is just an example. Actually the use case I had in mind is React server rendering. We may have different API for data fetching, cookies, etc. on the server and in the browser, but still write unified code using tasks, which we then run() with different contexts.
The alternative to context, and what is usually done with promises etc. is to use closures:
function createSomeTask(context) {
return Task.create(...).chain(...).map(...)...
}
// on server
createSomeTask(serverContext).run(...)
// in browser
createSomeTask(browserContext).run(...)
I want to run some computation multiple times with different parameters. How should I do that?
So the context feature should let you do that (though still not sure whether it's good idea) or you can just create two tasks.
Thanks, wrapping in a closure seems sufficient, IMO.