Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
How do I deal with async for a response in a callback function?

I'm using this package as an API wrapper to interact with the CampaignMonitor API within a serverless function. The objective is to return a response to the caller of the serverless function, confirming whether the operation of adding a subscriber to CampaignMonitor was successful or not.

Here is what I have so far:

exports.handler = async (event, context) => {
  const body = JSON.parse(event.body);

  // set request details
  const listId = process.env.CM_LIST_ID;
  const details = body;

  // Send Request and check for error returned
  api.subscribers.addSubscriber(listId, details, (err, res) => {
    if (err) {
      return {
        statusCode: 400,
        body: JSON.stringify({ message: err }),
      };
    } else {
      return {
        statusCode: 200,
        body: JSON.stringify({ message: 'success' }),
      };
    }
  });
};

Unfortunately, this doesn't work, I think due to the fact that there is no await for the response to the final part where the request is sent. I'm a little unsure of how to handle it, with it being a callback function.

I've been playing with this code for a little while now and, if there is no error, the subscriber is added to the subscriber list and a success response is returned from the serverless function when the second return statement is outside of the callback (below api.subscribers.addSubscriber).

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Problem

Returning a value from the callback function of api.subscribers.addSubscriber doesn't make it a return value of the wrapper handler function.

Solution

As the library you are using doesn't provide a promise-based API, you can create a promise wrapper around it and use that to get the desired result.

Easiest way to create a promise wrapper around something in nodeJS is to use the built-in util module.

const util = require("util");

const promisifedAddSubscriber = util.promisify(api.subscribers.addSubscriber);

Once you have a promise-based api.subscribers.addSubscriber function, you can await the call to this function.

exports.handler = async (event, context) => {
  try {
    ...

    // explicitly bind "this"
    const result = await promisifedAddSubscriber.bind(api.subscribers)(listId, details);
  
    return {
      statusCode: 200,
      body: JSON.stringify({ message: 'success' }),
    };
  }
  catch (error) {
    return {
      statusCode: 400,
      body: JSON.stringify({ message: error }),
    };
  }
};
about 4 years ago · Juan Pablo Isaza Report

0

You need to return a Promise from check_saved_url. Inside of the Promise, you then need to use resolve to replace return. You can also use reject(new Error("error")) if there was an error.

You can read more about promises on MDN

exports.handler = async(event, context) => {
    return new Promise((resolve, reject) => {
        const body = JSON.parse(event.body);

        // set request details
        const listId = process.env.CM_LIST_ID;
        const details = body;

        // Send Request and check for error returned
        api.subscribers.addSubscriber(listId, details, (err, res) => {
            if (err) {
              reject(new Error({
                  statusCode: 400,
                  body: JSON.stringify({
                    message: err
                  })
                }));
              } else {
                resolve({
                  statusCode: 200,
                  body: JSON.stringify({
                    message: 'success'
                  })
                });
              }
            });
        });
    };

about 4 years ago · Juan Pablo Isaza Report

0

find working implementation of async-await below:

exports.handler = async (event, context) => {
  try
  {
      const body = JSON.parse(event.body);

      // set request details
      const listId = process.env.CM_LIST_ID;
      const details = body;
    
      const result = await api.subscribers.addSubscriber(listId, details);
      return {
        statusCode: 200,
        body: JSON.stringify({ message: 'success' }),
      };
  }
  catch(err){
      return {
      statusCode: 400,
      body: JSON.stringify({ message: err }),
    };
  }
};
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!