Search code examples
javascriptnode.jsasync-awaitamazon-snsaws-sdk-js

Wait for AWS SNS publish callback to return a value to calling method


I am attempting to send a text message when a user requests to reset their password. I would like to wait for the message to be sent to alert the user if it was successful or not. I am currently attempting to do it as follows:

async function sendResetPasswordTextMessage(req, res) {
    let result = {};

    let phoneNumber = req.body.phoneNumber;                

    if (phoneNumber === undefined) {                       
        return sendInvalidParametersMessage(res);          
    }                                                      

    phoneNumber = phoneNumber.toString();                  

    const userProfile = await models.UserProfile.findOne({ 
        where: {                                           
            phoneNumber: phoneNumber                       
        }                                                  
    });                                                    
    ************************** RELEVANT CODE TO ISSUE *************************
    if (userProfile) {
        const message = "Your username is:\n" + userProfile.username;
        const sent = await AWSSNSClient.sendMessage(message, phoneNumber);

        if (!sent) {
            result.error = setTitleAndMessage("Error", "An error occurred");
        } else {
            result.success = setTitleAndMessage("Success", "Message sent"); 
        }
    }
    return res.send(result);
    ***************************************************************************
}

In my other class AWSSNSClient, I have the following sendMessage function:

function sendMessage(message, phoneNumber) {
    const params = { 
        Message: message, 
        MessageStructure: "string", 
        PhoneNumber: "+1" + phoneNumber
    };
    let sent = false;
    sns.publish(params, function(err, data) {
        if (err) {
            console.log(err, err.stack); // an error occurred
        }
        else {
            sent = true;
        }
    });

    return sent;
}

I cannot figure out how to make sendMessage wait for sns.publish to return before it returns itself. I have tried making it an async method and adding await on sns.publish, but the function still returns before sent gets set to true.

I know that the messages are sending without error because I am receiving them and no console logs are printed.


Solution

  • You can simply use callbacks for that. Modify your sendMessge like this

    function sendMessage(message, phoneNumber, cb) {
        const params = { 
            Message: message, 
            MessageStructure: "string", 
            PhoneNumber: "+1" + phoneNumber
        };
        sns.publish(params, cb);
    }
    

    then on your main file you can supply callback like this

    if (userProfile) {
      const message = "Your username is:\n" + userProfile.username;
      AWSSNSClient.sendMessage(message, phoneNumber, (err, data) => {
        if (err) {
          result.error = setTitleAndMessage("Error", "An error occurred");
        }
        else {
          result.success = setTitleAndMessage("Success", "Message sent");
        }
        res.send(result);
      });
    }