jaredly / jaredly/reason_async
Promise error handling
Nobody has claimed this yet.
- Dominant language
- OCaml
- Stars
- 67
- Forks
- 8
- PR merge metrics
- No merged PRs in 30d
Description
I'm opening this issue for discussion on the promise error handling.
The `Promise` module does not include support for rejections so far:
```reason
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](https://nodejs.org/docs/latest-v8.x/api/util.html#util_util_promisify_original) 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](https://github.com/laverdet/node-fibers/blob/4e31a39d35635251e8680301dccf7d186396f739/future.js#L250) (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!
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading the Promise and NodeContinuation modules, then compare their callback and rejection behavior with the Node promisify and JavaScript Promise conventions cited in the issue. The work is complete only after a clear rejection representation and handling API for Promise has been agreed and implemented consistently.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, node.js, ocaml
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100