-
Notifications
You must be signed in to change notification settings - Fork 8
Description
I'm opening this issue for discussion on the promise error handling.
The Promise module does not include support for rejections so far:
module Promise = {
type t('a) = Js.Promise.t('a);
let return = Js.Promise.resolve;
let map = (value, ~f) => value |> Js.Promise.then_(res => Js.Promise.resolve(f(res)));
let bind = (value, ~f) => value |> Js.Promise.then_(f);
let consume = (value, ~f) => value |> Js.Promise.then_(res => {f(res); Js.Promise.resolve(0)}) |> ignore;
let join2 = (a, b) => Js.Promise.all([|map(a, ~f=(r => Left(r))), map(b, ~f=(r => Right(r)))|]) |> Js.Promise.then_(items => Js.Promise.resolve((leftForce(items[0]), rightForce(items[1]))));
};Yet the NodeContinuation module returns Js.Result.t based on what was passed to the callback. If we promisify such callback, we'll get promise resolution or rejection. I'm not sure if it's node specific or general JS pattern. Actually then supports 2 callbacks and it's recommended to use second one in some articles:
p.then(onFulfilled[, onRejected]);
p.then(function(value) {
// fulfillment
}, function(reason) {
// rejection
});
May be that should be the way for Promise module, so that it returns Js.Return.t based on what callback was invoked akin to NodeContinuation? Probably reject type should be exn.
ES7 await just seems to throw rejection as exception, future.js (bundled with node-fibers) is also following the same pattern.
I've got a recommendation on the Discord to never reject promises unless it's fatal and just resolve them with result type of value. This works fine if you own all the code, but the benefit of using node ecosystem is leveraging large number of "batteries", and those do reject as a way of indicating errors. It makes sense to follow ecosystem conventions instead of fighting them and thus I'm thinking about a way to do that within reason_async.
P.S.: I've tried out this cool project recently and I really like the way it sugared promise code looks, very clean! Thanks for your work!