Simple Promise Resolving Bindings

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:

So far here is my binding for this function:

@bs.module("use-places-autocomplete")
external getDetails: string => Js.Promise.t<placeResult> = "getDetails"

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?

Thanks in advance!

1 Like

You need to ignore last promise. I use wait helper for it.

@alex.fedoseev - I just took a look and not sure I entirely follow, would you be able to show this in a simple example regarding the context above?

Much appreciated!

Got it!

Thanks so much @alex.fedoseev

1 Like

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)
  })
3 Likes

Thanks this is super helpful, I will give this a try to simplify the implementation.

1 Like