I have a function that I want to return a string. It makes a fetch request and the string is in the response. I've been using the standard .then(response => response.json()) format but so far the only way I've been able to actually get the string is to define an empty array before the fetch, push it into that, and return the whole array, but then when I try and extract it from the ensuing object it comes back undefined. Is there a better way? thanks
Asked
Active
Viewed 21 times
0
-
3Duplicate: [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Feb 15 '21 at 19:52
-
1Promise land is fun, before each then, there's a return if it's it is not null it's a value of some sort. This is chained to not fall out of order and so anything performed inside won't leak outside the promise unless you define a variable to catch the return for it (if any). – BGPHiJACK Feb 15 '21 at 19:54
-
1let variable = new promise... await chain of functions and on return, variable now has it. This is why it may have worked with a global array outside the promise to define while processing the promise but yeah read the above link. – BGPHiJACK Feb 15 '21 at 19:54