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

153
Views
Extracting data using fetch in Javascript

I am trying to read a locally stored text file using javascript. I need to pass the information stored on the text file to pass through evaluations for test result. Since I could not access the text file directly, I have hosted it on to a local server and trying to fetch data using the URL.

Here is how my code looks like -

const mediaFile = 'http://localhost:8080/' + fileName; 
function MediaInfo(file) {
  fetch(file)
  .then(function (response) {
    response.text();
  })
 .then(function (data) {
   console.log(data);
   return response.data();
 });
}

const a = MediaInfo(mediaFile);
await browser.expect(a).eql(44);

    });

But the calling of function MediaInfo(mediaFile) returns undefined. Any idea why I cant get the data returned??

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

0

The function inside the second then is returning some data, but MediaInfo itself isn't returning anything.

Since you have one await already, I'd suggest going fully async/await:

async function MediaInfo(file) {
  const response = await fetch(file);
  const text = await response.text();
  console.log(text);
  return text;
}

Also note that in order to avoid variables storing Promises, you'd probably want to change to

const a = await MediaInfo(mediaFile);
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!