Return a string from an Async function | Sololearn: Learn to code for FREE!

+7

Return a string from an Async function

How do I make the function in this code https://code.sololearn.com/WesZ28EZRWwj/?ref=app return the res variable, so whenever I call the function, I'll always get the returned value instead of a promise

11/7/2020 7:27:35 AM

Mirielle[ Left SL ]

35 Answers

New Answer

+15

I believe you have to use "await" when calling the function. In Python, it'd look something like async def func(): return 'blah' x = await func() Perhaps it's the same with JS. I could be wrong though.

+11

Burey i just got the idea that SL should have GitHub gist sync and the Code editor should be heavily improved

+10

Mirielle Ah, yeah. Forgot about that part. Hm. I seem to remember something about converting a promise into a generator and using "yield" .... but I'm not sure, and my skills in JS are fairly elementary. I'm sure someone will swing by and make me look stupid soon. :p

+10

I'll just add this to the thread: Promises is one of the most important concept a web developer should know and understand well. Whether it using the .then() function, using a callback and using async/await. Along with understanding data flows (synchronous and asynchronous) and how to update the UI after new data is fetched. Those are fundumental concepts that should be mastered and most importantly, understood.

+9

Burey I noticed the lack of "return" as well but assumed that either a) she was aware and aiming her question at a different part of the code, b) I suck at JS bad enough to not completely understand the code, or c) both a and b. Lol.

+8

Mirielle as you said, it wouldn't work. You should either call it inside an async function or use a .then(...) syntax. You can also use a self invoking async function: (async () => { const res = await loadFile(...); })();

+6

As long as you call the loadFile from within an async function you should be able to use await.

+5

I tried the self-invoking method and immediately `console.log(res) ` Just below the res declaration within the function but then, it was "undefined". Using .then() doesn't meet the requirement, I just want to returnd the value to where the function was called, something like let a = loadFile(url) console.log(a)

+5

Mirielle I'd like to see your workaround.

+5

Galaxy-Coding (use the search bar) if only SL had such feature πŸ™„

+4

Fox await keyword is valid only inside the async function, I don't think πŸ€” `await loadFile()` would work

+4

Mirielle it is undefined because the loadFile does not return anything. You need to return the promise. The await is what eventually resolves it.

+4

I deliberately remove the "return" statement because async function will always return a promise. I don't want a promise to be returned but a string, glad I'm now able to solve this with OOP by adding some functions to the prototype of the Promise object and the time complexity of the algorithm is ok

+4

Mirielle you are right about async function always returning a promise. What you can do is to *remove the async keyword from the function decleration, and return the promise. Then you should be able to use it. * you can also leave the async decleration as is. As long as you actively return the your own promise it should work.

+4

Oh, I see. You decided to use a callback function.

+3

The objective is to create a fileloader, then the client can simply call a function `getFile(name) ` to get their content

+3

Fox it's mainly hard to keep track of a constantly changed code πŸ˜…πŸ˜…πŸ˜…

+3

||| Fox it's mainly hard to keep track of a constantly changed code πŸ˜…πŸ˜…πŸ˜… Burey that’s why GitHub exists

+3

Hello Alaa Bashir This is not the right place to say β€œhi”, so you can message me with your doubts Also I want you to see this https://code.sololearn.com/Wv5gTHy1N6Ji/?ref=app https://code.sololearn.com/W26q4WtwSP8W/?ref=app https://www.sololearn.com/discuss/1316935/?ref=app https://www.sololearn.com/discuss/595802/?ref=app

+3

But why would FPS drop? Aren't resolve functions called before calling RAF? Then why the bottleneck? Right now i don't know wheter you used async function, Promise.all or something else, but the code could be facing some kind of memory leak, try using finally to clean up resources. Sorry, I know this doesn't help much, I'm not an expert on this.