Trying to create a web chat application where we fetch all the channels a user is a part of from Backend. Backend returns an array of objects containing twilio access tokens and channel names. After getting API response, Javascript iterates through the array and creates chat clients for each channel using following code:
let apiResponse = [
{token: 'abcdefgh', channel_name: 'a'},
{token: 'abcdef', channel_name: 'b'},
{token: 'abcd', channel_name: 'c'}
];
let createdClients = [];
apiResponse.forEach((item) => {
Chat.create(chatRoomToken)
.then(client => {
// You get the client here which can be pushed into createdClients array
})
}
Currently this is done in sequence as JS is single threaded and then as and when promises are resolved, createdClients
array is being populated . How can I possibly parallelise Chat.create(chatRoomToken)
for multiple channels in order to save more time. Has anybody solved this using web workers or service workers? Thanks.