异步函数调用 if/else 不返回值



我试图在 if/else 中提取异步值时陷入困境,我已经解决了许多错误,但以下内容返回空括号:

router.post('/api/shill', async (req, res) => {
  let checkIdLength = req.body.Id;
  let checkIP = validateIP(req.body.Ip);
  let checkPort = Number.isInteger(req.body.Port);
  console.log(req.body.Id);
  if (checkIdLength.length != 66 || checkIP != true || checkPort != true || typeof req.body.Wumbo != "boolean") {
    res.status(400).send('Invalid value(s) detected');
  }
  else try {
      challengeInvoice = getInvoice();
      res.status(200).send(challengeInvoice);
  } catch (e) {console.log(e)}
})
async function getInvoice() {
  await lnd.addInvoice({}, (err, res) => {return res}); 
}

FWIW,lnd.addInvoice与 grpc 调用相关联

你可以对异步数据使用承诺。

 try {
      getInvoice().then(challengeInvoice => {
           res.status(200).send(challengeInvoice);
       })
  } catch (e) {console.log(e)}

然后

    function getInvoice() {
        return new Promise( (resolve, reject) => {
          lnd.addInvoice({}, (err, result) => {
            if(err) {
               reject(err);
            }
            else{
              resolve(result)
            }
         }); 
       });
    }

最新更新