Reputation: 207
I'm trying to figure out how to make this Firestore Collection asynchronously. I currently have it in a recursive function because a for loop wasn't working either. I thought by using a recursive function it would give me more control for when I wanted to start the function over, but it's still not working. Please help. I know this would be something easy to solve for most people.
const fakeIt = async () => {
console.log("A");
var itemsCreated = 1;
items(0, itemsCreated);
async function items(num, itemsCreated){
if(num < itemsCreated){
console.log("B");
await db.collection('items').add({
active: true
}).then(async (item) => {
console.log("C");
items(num + 1, itemsCreated)
});
}else{
console.log("Done");
}
}
}
Array(2).fill(0).forEach(fakeIt);
The print out I'm getting is as follow:
A
B
A
B
C
Done
C
Done
I need the result of:
A
B
C
A
B
C
Done
Upvotes: 0
Views: 90
Reputation: 207
I figured it out by using a recursive function for the fakeIt loop. I got rid of all of the async-await and everything works perfectly. Thank to those that made the comments above.
function fakeIt(number, count) {
if (number < count) {
console.log("A");
var itemsCreated = 1;
items(0, itemsCreated);
function items(num, itemsCreated){
if(num < itemsCreated){
console.log("B");
db.collection('items').add({
active: true
}).then(async (item) => {
console.log("C");
items(num + 1, itemsCreated)
});
}else{
fakeIt(number+1, count)
}
}else{
console.log("Done");
}
}
fakeIt(0, 2)
Upvotes: 1