0

I was trying to find out how to get a txt file into javascript when I stumbled across this code:

const fun = async () => {
  const response = await fetch(file)
  return response.text()
}
const data = fun()
console.log(data)

I tried this code, and it works, but not exactly how I wanted it to. When I output it into the console, it gives me this:

enter image description here

with the .txt file in PromiseResult. I want the data variable to be a string of just PromiseResult, if that makes any sense. How can I do this? Thank you!

Sorry if some of the terms I use are wrong. I'm very bad at Javascript.

EDIT Btw, this is different from this question, as the code from that question stores the variable in a async function, which makes it a local variable, while I want my variable to be a global variable.

Wynder
  • 93
  • 8
  • 1
    use .then or async/await - like you do in `fun` – Jaromanda X Aug 16 '22 at 23:21
  • How would I use .then to accomplish this? – Wynder Aug 16 '22 at 23:31
  • `fun().then(data => console.log(data))` – Jaromanda X Aug 17 '22 at 00:07
  • [This answer](https://stackoverflow.com/a/28057246/5217142) in a llink of the accepted answer of the duplicate may be of help. Note the code :stumbled upon" in the post may be part of the problem - its not "work" and on the contrary is a prime example of the error addressed in the duplicate answer. – traktor Aug 17 '22 at 00:10
  • _"**EDIT** Btw, this is different from this question, as the code from that question stores the variable in a async function, which makes it a local variable, while I want my variable to be a global variable."_ Technically, no difference. – jabaa Aug 17 '22 at 00:15
  • @traktor From my understanding, if your question has a duplicate, the answer to the duplicate is supposed to answer your question, and if it doesn't, the two questions shouldn't be deemed as duplicates of each other. [This answer](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron/28057246#28057246) in the duplicate does not answer my question, so shouldn't be deemed as a duplicate. – Wynder Aug 17 '22 at 00:48
  • @Wynder in theory and in the best of worlds it wouldn't. But due to the large number of times this question has been asked, you are likely to be directed to a question or questions that may require research or investigation to see how they apply to a particular piece of code that is not handling asynchronous operations correctly. – traktor Aug 17 '22 at 01:01
  • This [answer](https://stackoverflow.com/a/14220323/16540390) answers your question. Look for the code `delay().then(function(v) { console.log(v); })` in the section **ES2015+: Promises with `then()`** and replace `delay` with `fun` and `v` with `data`. It's what @JaromandaX posted in the third comment. Same problem with different function and parameter names. ES2015+: Promises with then() – jabaa Aug 17 '22 at 01:10
  • @jabaa As I said in my edit, I want the variable, in your code the `v` variable, to be a global variable so that the variable can be accessed anywhere in the script, not just in the function. – Wynder Aug 17 '22 at 01:11
  • you can't reliably access an asynchronous result globally (ignoring the possibility of top-level await of course, but I doubt you are using ES modules) – Jaromanda X Aug 17 '22 at 01:13
  • Then make `data` a global variable and assign it in the `.then` block: `let data; fun().then(function(d) { data = d; })`. Stack Overflow isn't a code writing service. You have to invest some research effort: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users) – jabaa Aug 17 '22 at 01:13

0 Answers0