llrt icon indicating copy to clipboard operation
llrt copied to clipboard

Is top level await supported?

Open nocquidant opened this issue 11 months ago • 1 comments

Hi, is top level await supported?

The following code:

function sleep(ms: number) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}
await sleep(100);

export const handle = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    return response.success(200, { message: "hello world!" });
};

with the following config (node20):

esbuild ./src/handler.js --outfile=dist/handler.mjs --platform=node --target=node20 --format=esm --bundle --external:@aws-sdk --external:@smithy --external:uuid

results in cryptic Runtime.ExitError in logs...

{
    "time": "2024-03-01T20:14:59.260Z",
    "type": "platform.initRuntimeDone",
    "record": {
        "initializationType": "on-demand",
        "phase": "init",
        "status": "error",
        "errorType": "Runtime.ExitError"
    }
}

Thanks!

nocquidant avatar Mar 01 '24 20:03 nocquidant

Hi there! Not yet, top level is technically possible, but we have to do a significant engine update first so will take some time :) In the meanwhile, you can use a special async init function like so:

function sleep(ms: number) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}


export const init = async () => {
    // do init here
   await sleep(100);
}


export const handle = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
    return response.success(200, { message: "hello world!" });
};

richarddavison avatar Mar 01 '24 20:03 richarddavison

Closing as dup of https://github.com/awslabs/llrt/issues/124

richarddavison avatar Mar 13 '24 20:03 richarddavison

duplicating of myself?

nocquidant avatar Mar 15 '24 13:03 nocquidant

duplicating of myself?

Whoops, wrong link! Corrected now 🫡

richarddavison avatar Mar 15 '24 14:03 richarddavison