Javascript 异步/等待忽略返回

Javascript 异步/等待忽略返回,javascript,twilio,twilio-api,Javascript,Twilio,Twilio Api,我想向不同的人发送不同的短信,每次发送短信的呼叫都应该是同步的,我实现了async Wait,以使我的函数能够像这样工作,但由于某些原因,它不能像预期的那样工作 这是我的密码: 查询合格用户后 if(userQualifies) { try{ await insertIntoTable(); } catch(err) { console.log(err); } } async function insertIntoTable(){ try{

我想向不同的人发送不同的短信,每次发送短信的呼叫都应该是同步的,我实现了async Wait,以使我的函数能够像这样工作,但由于某些原因,它不能像预期的那样工作

这是我的密码:

查询合格用户后

if(userQualifies) {
   try{
      await insertIntoTable();
   } catch(err) {
      console.log(err);
   }
}

async function insertIntoTable(){
   try{
     await db.any(QUERY TO INSERT)
       .then(async function(idCreated){
          try{
             var params = {
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['1' + phone],
                 'body': message,
              }
              await sendMessage(params);
          }catch(error){
             console.log(error);
          }
       })

   } catch(err){
       console.log(err);
   }
}

async function sendMessage(params) {
   console.log('Im on sendMessage');
    return client.messages.create(params)
        .then( msg => {
            console.log("SUCCESS:");
        })
        .catch(err => {
            console.log("ERROR:");
        });
    console.log("message sent");
    return 'done';
}
当我运行此操作时,在插入到表中之后,我得到了sendMessage上的
Im的日志,但它没有发送消息,它忽略了
sendMessage()
函数的返回,并同时发送所有消息


当消息从
InsertintTable()
发送到
sendMessage()

时,我是否缺少使其发送消息的方法?我不太确定您的大多数代码都在做什么,但下面是异步模式,用于在启动下一个操作之前等待数组中的一个操作完成其异步操作:

async function sendMessage(params) {
   console.log('Im on sendMessage');
    return client.messages.create(params)
        .then( msg => {
            console.log("SUCCESS:");
        })
        .catch(err => {
            console.log("ERROR:");
        });
}

const messages = [{
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['1000000'],
                 'body': 'first message',
              },{
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['2000000'],
                 'body': 'second message',
              },
              {
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['3000000'],
                 'body': 'third message',
              }];

async function setMultipleMessages(messages) {
  for (let message of messages) {
    await sendMessage(message);
  }
}

setMultipleMessages(messages);

我不确定您的大多数代码都在做什么,但下面是异步模式,用于在启动下一个操作之前等待数组中的一个操作完成其异步操作:

async function sendMessage(params) {
   console.log('Im on sendMessage');
    return client.messages.create(params)
        .then( msg => {
            console.log("SUCCESS:");
        })
        .catch(err => {
            console.log("ERROR:");
        });
}

const messages = [{
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['1000000'],
                 'body': 'first message',
              },{
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['2000000'],
                 'body': 'second message',
              },
              {
                 'messagingServiceSid': 'XXXXXXXXXX',
                 'to': ['3000000'],
                 'body': 'third message',
              }];

async function setMultipleMessages(messages) {
  for (let message of messages) {
    await sendMessage(message);
  }
}

setMultipleMessages(messages);

当您使用
async
/
wait
时,不要使用
then
(也不要将
async
函数作为回调传递给它)。代码太不透明,无法理解。
db.any()?
.then
函数是用多个
idCreated
值快速连续调用的,还是仅用一个值调用的?我看不出一次发生多个
sendMessage
调用的机会。顺便说一句,在
sendMessage
中有两个
return
语句-第一个之后的一切都是死代码。您将只获得
成功
错误
控制台.log(“消息已发送”)永远不会发生。在使用
async
/
wait
时,不要使用
then
(也不要将
async
函数作为回调传递给它)。代码太不透明,无法理解。
db.any()?
.then
函数是用多个
idCreated
值快速连续调用的,还是仅用一个值调用的?我看不出一次发生多个
sendMessage
调用的机会。顺便说一句,在
sendMessage
中有两个
return
语句-第一个之后的一切都是死代码。您将只获得
成功
错误
控制台.log(“消息已发送”)永远不会发生。