How to perform task without waiting for another task to complete

I am using using socket to continuously push data to browser, however, I have to update history and send notification on some criteria which occurs some interval of time. When it goes to update history or notification function it takes some time to finish, until then socket is data is blocked to browser due to await keyword.

What if I don’t use the await keyword while calling update services, however, functions in the services are async/await, it will definately wait there, am I right?

I need that, socket should emit data continuously, if any conditions matches to updates services, then services should work in background

Or any suggestion where to handle such cases, may be I am incorrectly handling in socket library directly. (Note: I am not using Adonis built-in socket module)

the code

async sendMessage(data, loggedUser) {
    try {
        let sok_id = await socketServices.getByUserId(loggedUser);
        await io.to(`${sok_id.socket_id}`).emit('message', { content: data, importance: '1' });
        let hist = commonServices.updateHistoryAndIdleStatus(id, data)
        //   let hist = await commonServices.updateHistoryAndIdleStatus(id, data)
        let notify = commonServices.updateNotifications(id, data)
        //   let notify = await commonServices.updateNotifications(id, data) 
    } catch (err) {
        console.log("Err", err);
    } 
}

Well it’s not about Adonis, async/await is just a syntax sugar for good old (well, not so old) promises. Just use them.

Something like:

const tasks = [
commonServices.updateHistoryAndIdleStatus(id, data),
commonServices.updateNotifications(id, data)
];

const [result1, result2] = await Promise.all(tasks);
2 Likes