Monadic syntax to work with promise in ReasonML
This project is a minimal syntax extension that help you to be more happy, and keep every promise in a way.
npm install bs-promise-monad --save
simply
open BsPromiseMonad.PromiseMonad;
and enjoy life.
let mySweetenPromise = return(2);
mySweetenPromise
>>= (value => {
Js.log(value);
return(value + 2);
})
>>= (value => {
Js.log(value);
return(value + 3);
})
>>= (err => {
Js.log2("Failure!!", err);
return(-2);
});
Reject the promise
exception Sorry;
error(Sorry)
>>= (x => Js.log("do it " ++ x) |> return)
>>| (err => return(Js.log2("I handled this", err)));
exception Sorry;
let breakPromise = return("I'm trying...");
breakPromise
>>= ( x => Js.log("Program: " ++ x) |> return )
>>= ( y => error(Sorry) ) /* Reject here */
>>= ( z => Js.log("this is skipped")|>return)
>>| ( err => Js.log2("Handled error!!", err)|>return); /* Error handling here */
- v0.2.6 : update example on error handling
- v0.2.5 : add error handling (>>|)
- v0.2.0 : add Promise.reject by
error