'Why is this instance of a class being converted to a regular object in JavaScript?

I am making an app with WebSockets in Node.js. In order to run certain things asynchronously, I wrapped some async code in an async function and then ran it.

However, the issue is that whenever this portion is ran, the code throws an error that call.createTransport is not a function. This function is clearly defined in the Call class and works when I call it outside of this context.

I proceeded to log the call and found that before running the async function, the call object was an instance of Call, but inside the async function it is a regular JavaScript object, with the properties but not the methods defined.

I am completely stuck on this issue. I am running Node.js 16 LTS.

(async (call, produce, consume) => {
    let sendTransport;
    if (produce) {
        sendTransport = await call.createTransport();
    }
    let receiveTransport;
    if (consume) {
        receiveTransport = await call.createTransport();
    }
    return { sendTransport, receiveTransport };
})(client.call, produce, consume).then(({ sendTransport, receiveTransport }) => {
    callClients.set(sId, {
        ...client,
        produce: true,
        sendTransport,
        receiveTransport
    });
    s.send(JSON.stringify({
        type: CallEventType.TRANSPORT,
        data: {
            sendTransport,
            receiveTransport
        }
    }));
});


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source