Hi I am using Firebase for backend and I want to understand why this is just hanging in the terminal.
I am trying to seed my Authentication list and my Firestore db, so I created a seeding script. I read a list of users from a JSON file, and then I add them to Auth, however, in order to create a matching user record in Firestore, I retain the user ID that is returned to me from createUserWithEmailAndPassword to create a user object with first_name and last_name (trivial attributes).
Afterwards, I return that user object, and resolve all promises with Promise.all and then continue to use setDoc to add each of those user objects to Firestore.
Note: both of these promise arrays are created using map functions across the initial user list from JSON and then across the returned user object list from Auth, respectively.
The key point here is, even though everything is added to my Auth and Firestore just fine... the terminal stays hanging, to no avail...
const { store, auth } = require('../config.js');
const { createUserWithEmailAndPassword } = require('firebase/auth');
const { doc, setDoc } = require('firebase/firestore');
const data = require("./SEED_DATA.json");
console.log(data ? "DATA PRESENT" : "")
const seedUsers = async (users) => {
// seeds firebase Auth
const user_auth_hydrated = await Promise.all(users.map(user => {
return createUserInAuth(user)
}))
// seeds firebase Firestore
// we wait for auth because we want to retain the uids to create matching records in store
const user_store = await Promise.all(user_auth_hydrated.map(user => {
return createUserInStore(user)
}))
Promise.resolve(user_store)
}
const createUserInAuth = async ({first_name, last_name, email, password}) => {
return await createUserWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const {uid} = userCredential.user;
return {
ID: uid.toString(),
FIRSTNAME: first_name,
LASTNAME: last_name
}
})
.catch(err => console.log("USER ADD AUTH FAILED", err.code, err.message))
}
const createUserInStore = async ({ID, FIRSTNAME, LASTNAME}) => {
return await setDoc(doc(store, 'users', ID), {FIRSTNAME, LASTNAME})
}
seedUsers(data);
Picture of terminal just hanging post execution (everything is fine in Auth and Firestore):

You shouldn't mix traditional approach of promises and async / await syntax together. I advise to stick with async / await only.
The seedUsers async function returns undefined. Doing Promise.resolve(user_store) does nothing except creating an object that is destroyed at the end of the function.
You should fix your code as follows:
const seedUsers = async (users) => {
/* your logic */
return user_store;
}
and
const createUserInAuth = async ({first_name, last_name, email, password}) => {
try {
const userCredential = await createUserWithEmailAndPassword(auth, email, password);
/* your return logic */
} catch (error) {
/* your error logic */
}
}