Wait for transaction is not cross-browser compatible

Running the code below in Chrome, in a Vue 3 project with Vite.

const blockHash = await client.waitForTransactionFinalization(tx)

I received the following error:

GRPCClient.js:370 Uncaught (in promise) ReferenceError: setImmediate is not defined
    at GRPCClient.js:370:25

Debugging the waitForTransactionFinalization function I see the setImmediate() function in use

async waitForTransactionFinalization(transactionHash, timeoutTime) {
        assertValidHash(transactionHash);
        return new Promise(async (resolve, reject) => {
            const abortController = new AbortController();
            if (timeoutTime) {
                setTimeout(() => {
                    abortController.abort();
                    reject(new Error('Function timed out.'));
                }, timeoutTime);
            }
            const blockStream = this.getFinalizedBlocks(abortController.signal);
            const response = await this.getBlockItemStatus(transactionHash);
            if (response.status === 'finalized') {
                // Simply doing `abortController.abort()` causes an error.
                // See: https://github.com/grpc/grpc-node/issues/1652
                setImmediate(() => abortController.abort());
                return resolve(response.outcome.blockHash);
            }
            try {
                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                for await (const _ of blockStream) {
                    const response = await this.getBlockItemStatus(transactionHash);
                    if (response.status === 'finalized') {
                        setImmediate(() => abortController.abort());
                        return resolve(response.outcome.blockHash);
                    }
                }
                if (!abortController.signal.aborted) {
                    return reject(new Error('Unexpected end of stream.'));
                }
            }
            catch (error) {
                return reject(error);
            }
        });
    }

It seems that setImmediate() is not cross-browser compatible as we see on developer.mozilla

https://developer.mozilla.org/pt-BR/docs/Web/API/Window/setImmediate

How can I resolve this error?

I’m using the createConcordiumClient from lib @concordium/web-sdk version ^3.4.0

Thanks.

This is a bug in the library.

You cannot really fix it yourself until we have a new version of the library.

There is a PR fixing the issue, and we’ll let you know when a new version is released

Thanks for the report.

Hi @Santos

We have released @concordium/web-sdk version 3.4.1, which should fix this.

Let us know if you run into any other issues, and thanks again for the report.

1 Like

Thank you guys
It worked

1 Like