-
-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Converting from Js.Promise #64
Comments
My rule of thumb is:
[@bs.module "my-awesome-lib"]
external getStringAsync: unit => Promise.t(string) = "getStringAsync"
[@bs.module "my-awesome-lib"]
external _getStringAsync: unit => Promise.Js.t(string, Js.Promise.t) = "getStringAsync"
let getStringAsync = () => {
_getStringAsync()
->Promise.Js.toResult()
->Promise.mapError(error => `PromiseException(error))
} I also think in your case there is no need for I would implement // Note: Refactored in GH comment, may not compile :P
let authLink =
ApolloClient.Link.ContextLink.makeAsync((~operation as _, ~prevContext as ctx) => {
let token = Tokens.getUserToken();
switch(token) {
| None => Js.Obj.empty()
| Some(token) =>
let headers = Js.Obj.assign({
"authorization": "Bearer " ++ token
}, ctx.headers)
{"headers": headers}
}
}); This code may not express your logic in 100% but it's a simplified version. In my experience wrapping sync API into promises makes things worse 😉 |
@idkjs I believe @baransu has answered your question. In summary, your binding (ideally) wouldn't be written in terms of There are, however, conversions, in the common case that you do have two sets of code using the different types, and the README provides instructions on writing bindings for those cases where you can edit the code to replace |
Thank you, gentelman. Very instructive. Will revert with results. |
Greetings, @aantron.
Would it be possible on providing some guidance on converting from
Js.Promise
to reason-promise? It seems like in many cases, that is what someone might be trying to do.The library in question is reason-apollo-client. The branch has a bunch of other improvements, includes
reason-promise
as the defaultpromise
implementation. That branch is the next branch.By way of example, found at reason-promise-question, I have a
Js.Promise
function that gets me the current token value. It has the following type:The function can be found in Tokens and is reproduced below. This is a dummy function, there is no binding. The point is to see how to get it to compile.
When I try use this with
reason-promise
when creating anapollo/client
authlink
, I get the following error:Here is the
authlink
function:How do we convert this to a reason-promise? Please feel free to be as diadactic as you want to be.
Thank you, in advance.
I have reposted on stackoverflow if you prefer to answer there. The link is https://stackoverflow.com/questions/64294223/converting-from-js-promise-to-reason-promise-in-reasonml
The text was updated successfully, but these errors were encountered: