Repository: pedroslopez/whatsapp-web.js
File: src/structures/GroupChat.js (JavaScript)

Code before:
                }

                sleep &&
                    participantWids.length > 1 &&
                    participantWids.indexOf(pWid) !== participantWids.length - 1 &&
                    (await new Promise((resolve) => setTimeout(resolve, _getSleepTime(sleep))));
            }

            return participantData;
        }, this.id._serialized, participantIds, options);
    }

    /**
     * Removes a list of participants by ID to the group
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>}
     */
    async removeParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return (chat.groupMetadata.participants.get(lid._serialized) || chat.groupMetadata.participants.get(phone._serialized));
            });
            await window.Store.GroupParticipants.removeParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**
     * Promotes participants by IDs to admins
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
     */
    async promoteParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return (chat.groupMetadata.participants.get(lid._serialized) || chat.groupMetadata.participants.get(phone._serialized));
            });
            await window.Store.GroupParticipants.promoteParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**

Review comment (refactor):
We can simplify here for more readability:

```suggestion
                const id = window.Store.WidFactory.createWid(p);
                const lidOrWid = id.server === 'lid'
                    ? window.Store.LidUtils.getPhoneNumber(id)
                    : window.Store.LidUtils.getCurrentLid(id);
                    
                return chat.groupMetadata.participants.get(lidOrWid?._serialized);
            }).filter(p => Boolean(p));
```

Also the resulted array have to be filtered out of all the undefined objects that can be there.

Code after:
                }

                sleep &&
                    participantWids.length > 1 &&
                    participantWids.indexOf(pWid) !== participantWids.length - 1 &&
                    (await new Promise((resolve) => setTimeout(resolve, _getSleepTime(sleep))));
            }

            return participantData;
        }, this.id._serialized, participantIds, options);
    }

    /**
     * Removes a list of participants by ID to the group
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>}
     */
    async removeParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
            }).filter(p => Boolean(p));
            await window.Store.GroupParticipants.removeParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**
     * Promotes participants by IDs to admins
     * @param {Array<string>} participantIds 
     * @returns {Promise<{ status: number }>} Object with status code indicating if the operation was successful
     */
    async promoteParticipants(participantIds) {
        return await this.client.pupPage.evaluate(async (chatId, participantIds) => {
            const chat = await window.WWebJS.getChat(chatId, { getAsModel: false });
            const participants = participantIds.map(p => {
                const wid = window.Store.WidFactory.createWid(p);
                const lid = wid.server!=='lid' ? window.Store.LidUtils.getCurrentLid(wid) : wid;
                const phone = wid.server=='lid' ? window.Store.LidUtils.getPhoneNumber(wid) : wid;
                return chat.groupMetadata.participants.get(lid?._serialized) || chat.groupMetadata.participants.get(phone?._serialized);
            }).filter(p => Boolean(p));
            await window.Store.GroupParticipants.promoteParticipants(chat, participants);
            return { status: 200 };
        }, this.id._serialized, participantIds);
    }

    /**