0

I don't understand a lot about Promises. I have this function to read data in JSON file:

const getData = (fileName) =>
new Promise((resolve, reject) =>
  fs.readFile(fileName, 'utf8', (err, data) => {
  console.log("Tipo da data:", typeof(data))
  return err ? reject(err) : resolve(data);
  })
); 

I do this:

const hashList = getData('./users/hashList.json')
    .then(data => console.log(data))
    .catch(error => console.log('Error: ', error));

'./users/hashList.json' is something like that ["2d37c88b9e650846b5eb0d2c0f38ecbf8601f6c5"] I want to know if the hashReceived is in this hashList, I do this:

if(hashList.includes(receivedHash)){
console.log("ok")
}

But when I run the code, I have this error: TypeError: hashList.includes is not a function. When I console hashList, I receive this: Promise { } Someone would help me to resolve that as assynchronous function? I only resolve using readSync, is readSync the only way to resolve that?

  • Try running the "if" condition inside "then" – Ashik Paul Jun 11 '20 at 18:32
  • 2
    You know that `hasList` is just another promise, right? If you want to do something with your data, it must be done in the `then` callback. I believe you're attempting to combine asynchronous with synchronous programming and that's getting you confused. – Edwin Dalorzo Jun 11 '20 at 18:35

0 Answers0