Repository: dotnet/runtime
File: src/native/corehost/browserhost/loader/assets.ts (TypeScript)

Code before:
            if (!response) {
                response = {
                    ok: false,
                    status: -1,
                    statusText: "No response",
                } as any;
            }
        } catch (err: any) {
            response = {
                ok: false,
                status: -1,
                statusText: err.message || "Exception during fetch",
            } as any;
        }
        return response;
    }
}

// in order to prevent net::ERR_INSUFFICIENT_RESOURCES if we start downloading too many files at same time on a device with low resources
async function loadResourceThrottle(asset: AssetEntryInternal): Promise<Response> {
    while (throttlingPCS) {
        await throttlingPCS.promise;
    }
    try {
        ++currentParallelDownloads;
        if (currentParallelDownloads == loaderConfig.maxParallelDownloads) {
            dotnetLogger.debug("Throttling further parallel downloads");
            throttlingPCS = createPromiseCompletionSource<void>();
        }
        const responsePromise = loadResourceFetch(asset);
        const response = await responsePromise;
        dotnetAssert.check(response, "Bad response in loadResourceThrottle");

        asset.buffer = await response.arrayBuffer();
        return response;
    } finally {
        --currentParallelDownloads;
        if (throttlingPCS && currentParallelDownloads == loaderConfig.maxParallelDownloads! - 1) {
            dotnetLogger.debug("Resuming more parallel downloads");
            const oldThrottlingPCS = throttlingPCS;
            throttlingPCS = undefined;
            oldThrottlingPCS.resolve();
        }
    }
}

async function loadResourceFetch(asset: AssetEntryInternal): Promise<Response> {
    if (asset.buffer) {
        return <Response><any>{
            ok: true,
            headers: {

Review comment (suggestion):
The comparison operators should use strict equality (===) instead of loose equality (==) to follow JavaScript best practices and avoid potential type coercion issues.

Code after:

    async function loadResourceAttempt(): Promise<Response> {
        let response: Response;
        try {
            response = await loadResourceThrottle(asset);
            if (!response) {
                response = {
                    ok: false,
                    status: -1,
                    statusText: "No response",
                } as any;
            }
        } catch (err: any) {
            response = {
                ok: false,
                status: -1,
                statusText: err.message || "Exception during fetch",
            } as any;
        }
        return response;
    }
}

// in order to prevent net::ERR_INSUFFICIENT_RESOURCES if we start downloading too many files at same time on a device with low resources
async function loadResourceThrottle(asset: AssetEntryInternal): Promise<Response> {
    while (throttlingPCS) {
        await throttlingPCS.promise;
    }
    try {
        ++currentParallelDownloads;
        if (currentParallelDownloads === loaderConfig.maxParallelDownloads) {
            dotnetLogger.debug("Throttling further parallel downloads");
            throttlingPCS = createPromiseCompletionSource<void>();
        }
        const responsePromise = loadResourceFetch(asset);
        const response = await responsePromise;
        dotnetAssert.check(response, "Bad response in loadResourceThrottle");

        asset.buffer = await response.arrayBuffer();
        return response;
    } finally {
        --currentParallelDownloads;
        if (throttlingPCS && currentParallelDownloads == loaderConfig.maxParallelDownloads! - 1) {
            dotnetLogger.debug("Resuming more parallel downloads");
            const oldThrottlingPCS = throttlingPCS;
            throttlingPCS = undefined;
            oldThrottlingPCS.resolve();
        }
    }
}