I
I
Ivan Ivanovich2020-06-29 10:15:14
JavaScript
Ivan Ivanovich, 2020-06-29 10:15:14

Writing to a variable?

Hello.

There are 2 functions:

const getFile = async ()=>{
  return await fs.readFile('file')
  .then(data=>{ return data });
};

const getFileFunc = async ()=>{
  const file =  await getFile();
  console.log(file); //выводит прочитанный файл
  return file;
};


And I would like to write the result of getFileFunc to a global variable, but since node works asynchronously,

const file = getFileFunc();

you can’t do it simply, because this will return the promise in the pending status.

Can you please tell me if there is any way to write the result of getFileFunc to a global variable?

PS the fs module on promises is connected

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question