0

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??

Phil
  • 157,677
  • 23
  • 242
  • 245
pawanamigo
  • 35
  • 3

1 Answers1

1

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);
RShields
  • 316
  • 1
  • 8