More JS translation help

Here is the code I am looking at (it makes a HTTP request)

const https = require('https');

https.get('https://jsonplaceholder.typicode.com/users', res => {
  let data = [];
  const headerDate = res.headers && res.headers.date ? res.headers.date : 'no response date';
  console.log('Status Code:', res.statusCode);
  console.log('Date in Response header:', headerDate);

  res.on('data', chunk => {
    data.push(chunk);
  });

  res.on('end', () => {
    console.log('Response ended: ');
    const users = JSON.parse(Buffer.concat(data).toString());

    for(user of users) {
      console.log(`Got user with id: ${user.id}, name: ${user.name}`);
    }
  });
}).on('error', err => {
  console.log('Error: ', err.message);
});

So we have to create a Js.Array because the “push” modifies in place. The ReScript documentation for Js.Array is here: Js.Array | ReScript API

I can’t figure out how to create an object of type Js.Array .

I know that afterwards, we can dall Js.Array.push(data, …); but I don’t see how to create a blank Js.Array in the first place.

can use brackets?
[] ?

In rescript, you don’t have an array object. - or any other of similar kind

The idea is:

  • you have a valua of a specific type (e.g. a variable)
  • you have modules providing functions for values of a specific type

In this case you have the value data which is probably of type array<string> and a module Belt.Array providing functions for any type of array. (Js.Array and Js.Array2 are other modules containing functions for the array type.)
The syntax to instantiate a value of type array is [ <arrayElements> ]. Where <arrayElements is just a placeholder for any elements. - if there are any.

let data = []

data->Belt.Array.push("chunk")

Try on the playground