简体   繁体   English

JavaScript,异步返回function回调function

[英]JavaScript, return function callback in a async function

Is there any kind of performance problem by returning a callback function in a async function as this code?:作为此代码在async function 中返回callback function 是否存在任何性能问题?:

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);

I'm using Middy library, I don't think it's relevant to say but just in case.我正在使用Middy库,我不认为它是相关的,但以防万一。

You can return a promise or use callback.您可以返回 promise使用回调。

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

This is a common pain point with new comers to middy.这是 middy 新手的共同痛点。 This has been addressed in middy v2 with the deprecation of callbacks altogether.这已在 middy v2 中通过完全弃用回调得到解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM