I am looking for help trying to bind to a simple function from a javascript module to retrieve a promise and then resolve this promise within a rescript function.
Here is the function I am trying to bind to and resolve:
I sort of have it working, but confused to why I have to set the promise to a variable and then log that variable in order to have the handleSelection return a unit, otherwise it returns a promise, since Js.Promise.then_ needs to return the next promise by calling a Js.Promise.resolve within the then_ in order to chain the promises, but this seems odd to me… How can I “stop the chaining” and just return the raw data from the promise?
These hoops Js.Promise.t makes you jump through are a big reason I’ve started recommending the reason-promise library instead, particularly when writing custom bindings. It offers the ability to set custom error types in bindings and a unit-returning get function for side effects.
The code then becomes:
// this can be any opaque type you like
type getDetailsError = Js.Promise.error
@bs.module("use-places-autocomplete")
external getDetails: string => Promise.Js.t<placeResult, getDetailsError> = "getDetails"
let handleSuggestion = suggestion =>
getDetails(suggestion)->Promise.Js.get(d => {
Js.log(d)
onSelect(d)
})