In functional languages you usually don’t call methods on objects like response.json(), but use functions which operate on some data structure: Fetch.Response.json(response).
Hi, bs-fetch is the original Fetch API. It’s a package containing bindings, like the one you wrote by hand. One issue with your binding is that it’s not type-safe–the return type Js.Promise.t<'a> is too broad for the actual type, which is JSON. The return type should be Js.Promise.t<Js.Json.t>.
Hi, yawaramin, original fetch API’s json return type of Promise<any>https://fetch.spec.whatwg.org/#body-mixin so when i adjusted my code to your way, the compiler occur below error:
We’ve found a bug for you!
/mnt/f/Projects/meetingbook/src/FetchedDogPictures/FetchedDogPictures.res 23:12-26:13
Once you have a Js.Json.t, it’s a ‘raw’ object that you have to decode into a your application’s domain type. (In ReScript a JSON object is not just the same thing as a JavaScript object, this is for type safety reasons.)
Decoding can be done with a library like Decco, which basically provides a macro that looks at the structure of a type definition and generates a decoder for it.
A JavaScript “any” type isn’t exactly the same as a ReScript 'a type. Although 'adoes allow you to use “any” type, using 'a with an external binding will unsafely bypass type checking. I assume when the WhatWG spec says “any,” it really means “any JSON object.” There are JavaScript types that a Fetch json() method isn’t able to return.
In ReScript, the way you safely handle an external “any” type is to give it an opaque/abstract type and then use a function at runtime to safely cast it to the correct type. In fact, this is exactly what the Js.Json.t type is, and exactly what Decco does (along with other JSON decoding libraries). Js.Json.t essentially mean “any type that conforms to the JSON spec.”
Probably would have been better to start a new thread after 2 years: with additional infos on what you are trying to achieve and a code-snippet of the code having the described error.
This way it would be easier to help.
But the shortest possible reprosucible code with your described error is this:
let x =
The error states, you forgot to define ,what the actual value (expression) of a let binding should be.
You could fix the example above by adding the missing part: