等待 AWS SNS 发布回调向调用方法返回值 [英] Wait for AWS SNS publish callback to return a value to calling method

查看:37
本文介绍了等待 AWS SNS 发布回调向调用方法返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户请求重置密码时,我尝试发送短信.我想等待发送消息以提醒用户它是否成功.我目前正在尝试按以下方式进行:

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:
" + 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);
    ***************************************************************************
}

在我的另一个类 AWSSNSClient 中,我有以下 sendMessage 函数:

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

我不知道如何让 sendMessage 在返回之前等待 sns.publish 返回.我尝试将其设为异步方法并在 sns.publish 上添加 await,但该函数仍会在 sent 设置为 true 之前返回.

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.

推荐答案

您可以简单地为此使用回调.像这样修改你的sendMessge

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

然后在您的主文件上,您可以像这样提供 callback

then on your main file you can supply callback like this

if (userProfile) {
  const message = "Your username is:
" + 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);
  });
}

这篇关于等待 AWS SNS 发布回调向调用方法返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆