Reputation: 103
So the question is how to send messages in pubnub sequentially.
Example: I add text and 2 images. Flow should be like => send text as 1st message, send first image as 2nd message and send second image as 3nd message. They should appear in pubnub history one by one.
Let`s say I have all this stuff in sendArray variable
(async () => {
for await (const item of sendArray) {
pubnub.publish({
channel: currentChannel,
message: item,
})
}
})();
I`ve tried something like this, but it's not working.
Upvotes: 0
Views: 140
Reputation: 2241
This is almost correct, just a wrong placement of the await
keyword.
(async () => {
for (const item of sendArray) {
await pubnub.publish({
channel: currentChannel,
message: item,
})
}
})();
This is because the publish method is an async operation that you need to await. for await
is a special construct for iterating async iterables so there is no need to use it here. For loop respects the await keyword inside async blocks.
Upvotes: 2