I can’t figure out why the await function setDoc is not completed in my example below when I launch my react native app for the first time.
When I launch it a second time however, it works well.
Can you help me?
useEffect(() => {
registerForPushNotificationsAsync().then(async token => {
// The following gets called
console.log("Before await")
// The following does not complete when I first launch the app.
await setDoc(doc(db, "devices", token), { test: "test" })
.then(x => {
// The following does not get called
console.log('Sucess')
})
.catch(error => {
// The following does not get called
console.log('Error')
})
// The following does not get called
console.log("After await")
});
return () => {};
}, []);
with registerForPushNotificationsAsync defined outside useEffect as:
async function registerForPushNotificationsAsync() {
...
return token;
}
Thank you.
3
Answers
If there a reason why you want to use await there ?
Otherwise you should try to do this using only
.then
and syncronious code :Try moving the async function outside of the useEffect function:
use
async await
as follows.