I’m working on some bindings for a library that uses a higher-order, polymorphic callback for signifying done-ness of async actions. I’m struggling with how to model/deal with this in Reason/Bucklescript.
The function is used like so:
_(push => {
Promise.all([
axios.get(url1),
axios.get(url1),
])
.then(([ res1, res2 ]) => {
push(null, res1) // push a value
push(null, res2) // push a value
})
.catch(err => push(err, null)) // push an error
.finally(_ => push(null, _.nil)) // say "we're done here"
})
As you can see, push
has 3 potential values for its second argument: null
, some 'a
, and _.nil
.
I’ve modeled the null
and 'a
usages like below, but adding in the additional _.nil
variant has me stumped. I looked at [@bs.unwrap]
, but this doesn’t strike me as being useful for defining the type of push
.
type t('e, 'a);
type push('e, 'a) = (~error: 'e=?, ~value: 'a=?, unit) => unit;
[@bs.module] external generator: (push('e, 'a) => unit) => t('e, 'a) = "_";
Is there some idiomatic way to model this function?