Comments (3)
My rule of thumb is:
- when you know your promise won't fail (and the promise is just to express async) just use
Promise.t('a)
when binding.
[@bs.module "my-awesome-lib"]
external getStringAsync: unit => Promise.t(string) = "getStringAsync"
- when I have no idea about the internals of the function I'm binding to I would use
Promise.Js.t('a, Js.Promise.error)
and convert toResult.t
as soon as as possible by wrappingJs.Promise.error
into poly variant for better error composability. I believe this can be achieved withcatch
as well and I'm not 100% if its correct.
[@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 Token
module to be async as the JS API is sync.
I would implement authLink
like this:
// 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 😉
from promise.
@idkjs I believe @baransu has answered your question. In summary, your binding (ideally) wouldn't be written in terms of Js.Promise
, but in terms of reason-promise to begin with. Js.Promise
from BuckleScript and reason-promise are just two incompatible types (deliberately, as the first one is not type-safe), so you can't use one where the other is expected, which is what your code appears to have been trying to do.
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 Js.Promise
by reason-promise already at the binding level, thus avoiding introducing any Js.Promise
s at all. @baransu has pretty much perfectly explained how to use the binding support in his comment (thanks!).
from promise.
Thank you, gentelman. Very instructive. Will revert with results.
from promise.
Related Issues (20)
- BuckleScript 8.0.0 breaks listToArray / Promise.all HOT 5
- Reduce compiled size again HOT 4
- Format with refmt? HOT 2
- About the deprecation of the infix operators HOT 2
- Promise.allOkArray never resolves if input is an empty array HOT 1
- Tests: remainsPending appears not to be working
- Convert tests to OCaml syntax
- Add a js_of_ocaml variant
- Try an internal module with double underscores to hide "rejectable" from error messages
- Exceptions cause resolution with `undefined` HOT 8
- Provide type abbreviation for promises that reject with Js.Promise.error
- Convert README snippets to ReScript syntax
- Convert to ReScript syntax once there is enough adoption HOT 10
- tap on a rejected promise triggers unhandled promise rejection HOT 3
- Native Example HOT 4
- Example for reason syntax with letop
- Promise.pending/Promise.exec/Promise.resolved
- Align interfaces between native and js versions
- Melange compatibility HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from promise.