0

callback将函数中的async函数作为此代码返回是否存在任何性能问题?:

import middy from '@middy/core';
import someFunction from 'someFunction';

async function testFunction (
  args,
  callback
) {
  // code
  const data = await someFunction();

  return callback(null, {
    statusCode: 200,
    body: JSON.stringify(data)
  });
}

export const handler = middy(testFunction);

我正在使用Middy库,我认为这无关紧要,但以防万一。

4

1 回答 1

1

您可以返回一个承诺使用回调。

const testFunction = async (event, context) => {
  // ... other logic
  return {
    statusCode: 200,
    body: JSON.stringify(data)
  }
}

这是 middy 新手的常见痛点。这已在 middy v2 中解决,完全弃用回调。

于 2021-01-18T07:36:42.923 回答