So it's been hours and hours i'm breaking my head to get this async schema working:
1) I have a function prepare message which is async as it needs to get download an attachment
2) I have a function send message which calls gmail api and sends the prepared message This function returns a callback function which i'm willing to use onSuccess
3) I have a loop for which calls this sendMessage function, which calls the preparemessage function
Everything works as expected, the loop creates N calls with N answers However the callback function is apparently configured before and gets the result of the last value in the for.
This is part of the code:
arrayOfMessagesToBeSent=[{msg1},{msg2}...] (got from the store)
for (const x of arrayOfMessagesToBeSent) {
store.actions.gapi.sendMessage(
await store.actions.mailing.prepareMsg(
{messageStuf}
),
(answer) => {
console.log("call:", x.id);
store.actions.mailing.sendCallback(answer, x.id);
};
);
}
Once prepare message is finished, the message is sent. I get one valid answer per email sent However the loop for keeps running in the background and overrides that x.id value
store.actions.mailing.sendCallback(answer, x.id); => Answer follows the good flux => Answer is different for each message => x.id gets overrided and all the times this function is executed, gets only the x.id
Do you know how could I synchronize it? So basically i want the callbackfunction (or that x.id variable) to wait until prepare message is finished.
Option A: The best one: Messages get ready while others are being sent. This would be the best solution
Option B: If it is impossible, at least the flux:
1)prepare 2)send 3)callback and then go with second message 1)prepare 2)send 3)callback... would be good enough
Note: I don't want to use promise-all with a .map(). I dont want to prepare ALL messages and then send ALL messages, a loop for and option B would be a better implementation for my use case.
Thank you!