minimal result reification
Results are an attempt to re-build the call stack conceptual model back on top of the "event loop" model. The approach they take is to ask you to reify your asynchronous function calls with a Result instance. The intention of these is to model stack frames, in that they will eventually be either a successfully computed value or an error. Because these Result instances are runtime objects you can compose them together to recreate the computation tree that is normally implicit and maintained underneath the runtime.
This implementation does nothing to improve the stack traces of your errors but that feature could be added.
With your favorite package manager:
packin add result-core
component install jkroso/result-core
npm install result-core
then in your app:
var Result = require'result-core'
A class for creating concrete representations of function calls which can be manipulated programmatically at run-time.
The state of the result. Can be one of:
All Results start of in a "pending" state and will transition either to "done" or "fail".
state // => 'pending'
The value of the Result. If the Result has a state of "fail" then this value is an error. If its "pending" then reading its value is useless. You should use the read method unless you know what your doing and are handling each of the 3 possible states correctly.
var result =resultwritea + breturn resultadd1 2value // => 3
give the Result its value and change its state to "done"
var one = write1onestate // => 'done'onevalue // => 1
give the Result its error and change its state to "fail"
var err = 'coz oops'var two = .errorerrtwostate // => 'fail'twovalue // => err
access the result of
onereadn // => 1// error handlers are optionaltworeadnull // value handlers are also optionale // => err