Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Building an Object from fetch statement

I have some code that when you console.log it, it looks like the image below:

Console.log output

The code I am running is as follows:

onClick={() => {
                    const stream = fetch(
                      'https://lichess.org/api/games/user/neio',
                      { headers: { Accept: 'application/x-ndjson' } }
                    );

                    const onMessage = obj => {
                      console.log('test', obj);
                    };

                    const onComplete = () =>
                      console.log('The stream has completed');

                    stream.then(readStream(onMessage)).then(onComplete);
                  }}
export const readStream = processLine => response => {
  const stream = response.body.getReader();
  const matcher = /\r?\n/;
  const decoder = new TextDecoder();
  let buf = '';

  const loop = () =>
    stream.read().then(({ done, value }) => {
      if (done) {
        if (buf.length > 0) processLine(JSON.parse(buf));
      } else {
        const chunk = decoder.decode(value, {
          stream: true,
        });
        buf += chunk;

        const parts = buf.split(matcher);
        buf = parts.pop();
        for (const i of parts) processLine(JSON.parse(i));
        return loop();
      }
    });

  return loop();
};

export default readStream;

What I am trying to do is build a parent object that contains all these individual rows of data.

I'm new at promises and fetch etc. So currently, I have no idea on how to build this parent object that contains each individual row.

Any suggestions?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Can't you have a global array and add items to it like:

var arrCollection = [];

...

const onMessage = obj => {
    arrCollection.push(obj);
};

You can have an object with those items doing like:

var objCollection = { items: arrCollection };
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!