I have a lot of mongoDB resources collections. All data i show in table. You can simultaneously change the data in the table in different collections. To do this, I drag the changes to the batches and call the change in each batch. I also collect the names of the collections whose data I want to update in parallel. Example:
switch (brand) {
case 'example_brand':
if (!promisesNames.includes('example_brand')) {
promisesNames.push('example_brand');
promises_triggerData.push(tickets_data_crm_example_brand.trigger(additionalScope))
}
return example_brand_edit.trigger(additionalScope);
where promises are triggers for changing data, and promises_triggerData is used to call the corresponding collections. I'm attaching a screenshot where you can see that I called 6 promises to change data and one to call data, but for some reason the data call comes before the data change. Why is this happening?
You can you add an event handler on these calls so once everything is done running it hits your data call after. Also checking to make sure its set to manually run so it wont run outside of when you need it.
I see where you’re coming from, and I appreciate your input. The reason why all the promises seem to trigger at once is due to how Promise.all() works. When you call Promise.all(), all the promises inside it are initiated at the same time, and they run in parallel. This means that even if you use then(), the individual promises don’t necessarily resolve in the order they were triggered, because they are asynchronous and can complete at different times based on how long each operation takes.
To make sure the data changes completely before the data calls, you could chain the operations so that the data changes are fully finished before starting the data calls. Please provide your code if you want it to be more clear to me next time. I was talking about the on Success event handlers built into queries in retool. Not necessarily an event handler in code. Sorry about my confusion.
Also just some advice to take or leave, real development is about not assuming other people's suggestions as being less syntactically correct, especially when many times it comes down to a lack of context and/or lack of communication. And in times you are correct, using those instances as opportunities to provide growth for others. Especially because @TRF is a great developer and is always just trying to help; as we all should be. You'll also receive a lot more replies from others if you aren't seen as notoriously stubborn or rude.
It looks like there’s been a bit of misunderstanding, and I think it’s a great opportunity to step back and remember what we’re all here for—helping each other grow as developers.
@Vlad_Posibl, from looking at the slice of code you shared, and based on what you're experiencing, it seems that within the if statement, you are collecting the promise from tickets_data_crm_example_brand.trigger(additionalScope), with the intent to resolve it after the six promises for data changes are completed. However, .trigger() actually initiates the query immediately, which explains why you’re seeing it finish before the other promises in the console.
This may not fit directly in your code as you may want to trigger tickets_data_crm_example_brand conditionally but we can definitely help you find a way to implement this.
Its just the same and in both variants it does not work as it should. That is the problem. The second await is triggered before the first await has finished.
// in the correct scope:
let needToFetch = false // to fetch data later only if we need to.
let additionalScopeObj = {} // in case we need to shape the additionalScope within deeper scopes.
//...
switch (brand) {
case 'example_brand':
if (!promisesNames.includes('example_brand')) {
promisesNames.push('example_brand');
// promises_triggerData.push(tickets_data_crm_example_brand.trigger(additionalScope))
// remove the line above from the if statement. This is what's triggering the query early.
needToFetch = true // we need to fetch later
additionalScopeObj = {...yourParams} // shape as needed
}
// ... rest of your code
// "At the end, I display this data:"
// return Promise.all(promises).then(() => Promise.all(promises_triggerData));
// replace with:
await Promise.all(editPromises)
if (needToFetch){
await tickets_data_crm_example_brand.trigger(additionalScopeObj)
}