+ 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
Mirielle I'd like to see your workaround.
+ 5
Galaxy-Coding (use the search bar) if only SL had such feature 🙄
+ 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
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
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.
+ 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())
+ 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
+ 1
Fox Indeed there are no return statements.
+ 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