This is my current code (see image)
I need my OwnProfile function to wait for my GetUsername function to finish reading data from firebase before continuing.
I tried async await, but it is not working as shown in the terminal where line 52 and 54 are used before GetUsername has gotten the data.
Anyone got any tips to fix this?
3
Answers
you need to await GetUsername method so that console.log could get executed once data has response
Since you’re working with a callback function in
GetUsername()
, you must return aPromise
which is to be resolved in the callback function code, like this:With this you should be able to do
Note: This is only an incomplete example. Take care of calling
reject()
in case an error occurs.If you check the reference docs for
on
you’ll see that it doesn’t return aPromise
, so you can’tawait
its results. Useonce
(orget
) instead: