+ 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.
7th Nov 2020, 7:40 AM
Fox
Fox - avatar
+ 11
Burey i just got the idea that SL should have GitHub gist sync and the Code editor should be heavily improved
8th Nov 2020, 5:15 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 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
7th Nov 2020, 7:50 AM
Fox
Fox - avatar
+ 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.
8th Nov 2020, 4:35 PM
Burey
Burey - avatar
+ 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.
7th Nov 2020, 9:10 AM
Fox
Fox - avatar
+ 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(...); })();
7th Nov 2020, 7:53 AM
Burey
Burey - avatar
+ 6
As long as you call the loadFile from within an async function you should be able to use await.
7th Nov 2020, 8:40 AM
Burey
Burey - avatar
+ 5
Mirielle I'd like to see your workaround.
7th Nov 2020, 11:13 AM
Kevin ★
+ 5
Galaxy-Coding (use the search bar) if only SL had such feature 🙄
8th Nov 2020, 4:31 PM
Burey
Burey - avatar
+ 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.
7th Nov 2020, 8:38 AM
Burey
Burey - avatar
+ 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.
7th Nov 2020, 11:54 AM
Burey
Burey - avatar
+ 4
Oh, I see. You decided to use a callback function.
8th Nov 2020, 7:07 PM
Kevin ★
+ 3
Fox it's mainly hard to keep track of a constantly changed code 😅😅😅
7th Nov 2020, 9:16 AM
Burey
Burey - avatar
+ 3
||| Fox it's mainly hard to keep track of a constantly changed code 😅😅😅 Burey that’s why GitHub exists
8th Nov 2020, 2:12 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
9th Nov 2020, 6:52 PM
Galaxy-Coding (inactive)
Galaxy-Coding (inactive) - avatar
+ 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.
10th Nov 2020, 8:59 AM
Kevin ★
+ 2
Mirielle Idk why you wouldnt want to use .then(). You can do this. const getFile = (url) => { return fetch(url) .then(data => data) .catch(err => { console.log(err) return "Some error" }) } And then create the async function. const fileLoader = async () => { const result = await getFile(url) console.log(result) } You can parse the response in the fetch, using .then(data => data.json())
7th Nov 2020, 9:01 AM
Dovlet Hallayev
+ 2
Mirielle Kevin ★ it's better moves to promises and rective programing. See RxJS https://github.com/ReactiveX/rxjs but for that, you need know how events an observers/subscriber software patterns works. Only like this, you can decouple your game logic from the ui
8th Nov 2020, 10:58 PM
David Ordás
David Ordás - avatar
+ 1
Fox Indeed there are no return statements.
7th Nov 2020, 9:17 AM
Dovlet Hallayev
+ 1
If you are using "new Promise".... why you don't use "fetch(...)"? Are part of the same ECMA specification version. https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API
7th Nov 2020, 2:01 PM
David Ordás
David Ordás - avatar