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.
This module is simple and self-completed. It means that you can use it with confidence that it won't bring any breaking changes in a subsequent update.
npm install bs-promise-monad --save
and add
bs-promise-monad
into bsconfig.json.
simply
open 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);
});
/* With auto binding */
mySweetenPromise
>>- (value => {Js.log2("from first promise:",value); value+2})
>>- (value => {Js.log2("from second promise:", value); value+3})
>>- (err => {Js.log2("Handled error", err); err})
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 */
breakPromise
>>- ( x => Js.log("Program: " ++ x))
>>- ( y => error(Sorry) )
>>- ( z => Js.log("this will be skipped"))
>>/ ( err => Js.log2("Handled error!! without explicit return", err));
let sequenceStart = return(7);
breakPromise >>* [
(x => x+2),
(x => x*3),
(x => x+4)
]
/** With a final promise binding */
mySweetenPromise
>>- (value => {Js.log2("from first promise:",value); value+2})
>>- (value => {Js.log2("from second promise:", value); value+3})
>>. (value => Js.log2("This is my last promise", value))
>>.
operator to ignore the final promise( to use in the last step )>>*
operatorerror