0

I have a function that returns a promise, resolving three values, but these values are constructed, and resolved, in a nested function. In chrome this works and the correct values are resolved, however in Firefox and safari the function returns without resolving the promise. I have put a breakpoint in the callback function, and in chrome it hits every time, but passes over it in Firefox. my suspicion is that chrome is executing the code-block until it reaches the resolve but in Firefox it is returning early.

const getConsumer = (
    socket,
    consumerTransport,
    device,
    producerId
) => {
    return new Promise((resolve) => {
        const message = {
            rtpCapabilities,
            producerId,
            consumerTransportId
        };
        const callback = async function (consumeData) {
            const { id, kind, rtpParameters } = consumeData;
            const consumer = await consumerTransport.consume({
                id,
                producerId,
                kind,
                rtpParameters,
                appData: {}
            });

            const stream = new MediaStream();
            stream.addTrack(consumer.track);
            resolve({
                consumer,
                stream,
                kind
            });
        };
        socket.emit('consume', message, callback);
    });

I've attempted to re order the function so the promise is inside the callback function but this means I can't call 'then' where the getConsumer function is invoked, I have also tried nesting a second promise in the callback function, but to no avail.

Daniel
  • 1
  • have you tried wrapping the `consumerTransport.consume` call in a try catch block? Maybe there's some error there and thats why promise is not getting resolved. – Cybershadow Mar 04 '21 at 09:06
  • 1
    Promises *are not synchronous*. If you have a function that returns a promise, then it will be *eventually* resolved but the function always returns a pending promise. I'm omitting some edge-ish cases that aren't related to your use case. I suspect you're just seeing [Weird behavior with objects & console.log](https://stackoverflow.com/q/23429203) because the console is lazy. Furthermore, there is no standard for consoles, so it is possible that in one console tells you a promise is fulfilled another says it's pending and both of these are correct because they show state from different times. – VLAZ Mar 04 '21 at 09:46

0 Answers0