Looking for a working example for Fetch + Promise

Hi,

I am exploring ReScript (I looked at ReasonML a few years ago).
I am trying to write a simple program to combine Fetch with Promise.

Some code I found on the site documentation:

Js.Promise.(
  Fetch.fetch("/api/hellos/1")
  |> then_(Fetch.Response.text)
  |> then_(text => print_endline(text) |> resolve)
);

gives me a syntax error

   9 ┆
  10 ┆ Js.Promise.(
  11 ┆ Fetch.fetch("/api/hellos/1")
  12 ┆ |> then_(Fetch.Response.text)
  13 ┆ |> then_(text => print_endline(text) |> resolve)

  I'm not sure what to parse here when looking at "(".

Am I missing something?
Is the ReScript compiler different?

Looks like Module.() is not a valid ReScript syntax. The playground compiles your example to:

{
  open Js.Promise
  Fetch.fetch(@reason.raw_literal("/api/hellos/1") "/api/hellos/1")
  |> then_(Fetch.Response.text)
  |> then_(text => print_endline(text) |> resolve)
}

Thank you @hoichi.

I had to make a few tweaks to make it work since fetch is not part of nodeJS.

// test.res
%%raw(`
const fetch = require('node-fetch');
`)

  open Js.Promise
  Fetch.fetch("https://type.fit/api/quotes")
  |> then_(Fetch.Response.text)
  |> then_(text => print_endline(text) |> resolve)

npm install node-fetch to install fetch for nodeJS.
npm run build to compile and node src/test.bs.js to run.

It is still mentioned on the rescript doc though (here). I guess it would worth opening an issue on the doc repo

EDIT: Issue 26

1 Like