This is my code:
let curId;
if (!user) {
db.collection("users")
.add({
name: name,
})
.then((doc) => curId = doc.id)
.catch((err) => console.error(err));
} else {
curId = user.id;
}
console.log(curId); <-- I need curId here
Currently I get undefined for curId
because I am not waiting for the async code to finish running. What is the best way to achieve this?
Some approaches I've thought of:
- Use
await
instead of.then
but I feel like this may look less organized/messy with thetry
andcatch
- I could also add what I want to do with
curId
in both theif
and theelse
statement but I'd rather not write redundant code