-1

i need help. I research about this topic too much but i don't find any solution.

We can list all clients of a Room with code below;

io.of('/').in('room').clients((error, clients) => {
    if error throw error;
    console.log(clients);
});

My question is how can I show this clients result in a variable.

I tried these solutions: 1)

var list = io.of('/').in('room').clients ((error, clients) => {
    if error throw error;
    return clients;
}); // didn't work

2)

async function getList() {
    return await io.of('/').in('room').clients((error, clients) => {
        if error throw error;
        return clients;
    });
}
n3pixowe
  • 122
  • 10

1 Answers1

4

Just wrap the code inside a Promise

function getClients() {

    return new Promise((resolve, reject) => {
        io.of('/').in('room').clients((error, clients) => {
            if(error)
                return reject(error);

            resolve(clients);
        });
    });

}

(async() => {
    const clients = await getClients();
})();
Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98