for await()

in deno1.3.0/lib/launcher.js [21:64]


for await (const line of readLines(Deno.stdin)) {
  let response = {error: "couldn't execute your function"};

  try {
    const input = JSON.parse(line);
    let payload = {};
    for (const [key, value] of Object.entries(input)) {
      if (key === 'value') {
        // extract the payload
        payload = value;
      } else {
        // set the env variables
        Deno.env.set('__OW_' + key.toUpperCase(), value);
      }
    }

    const sourceCode = './ow_bundle.js';
    if (await exists(sourceCode)) {
      const {default: main} = await import(sourceCode);
      response = await main(payload);
      if (Object.prototype.toString.call(response) !== '[object Object]' && Object.prototype.toString.call(response) !== '[object Array]') {
        response = {
          error: 'response returned by the function is not an object'
        };
        console.error(response);
      }
    } else {
      response = {
        error:
          "couldn't find the bundled file. There might be an error during bundling."
      };
    }
  } catch (error) {
    console.error(error);
    response = {
      error: error.message
    };
  }

  await Deno.writeAll(
    output,
    new TextEncoder().encode(JSON.stringify(response) + '\n')
  );
}