pmiranda
pmiranda

Reputation: 8420

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?:

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.

Upvotes: 0

Views: 770

Answers (1)

will Farrell
will Farrell

Reputation: 1759

You can return a promise or use callback.

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. This has been addressed in middy v2 with the deprecation of callbacks altogether.

Upvotes: 1

Related Questions