Javascript 如何从';它在另一个函数中?

Javascript 如何从';它在另一个函数中?,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我知道有很多关于异步编程和承诺的问题被问到了,但是我真的需要一个带有这个特定代码的示例来弄清楚我应该如何返回在另一个函数中返回的承诺 我有两个功能。第一个是在到达一条路线时。此路线应创建付款链接并将预订保存到数据库 exports.create_booking = function(req, res) { req.body.payment = exports.create_booking_payment(req.body.total_amount); console.

我知道有很多关于异步编程和承诺的问题被问到了,但是我真的需要一个带有这个特定代码的示例来弄清楚我应该如何返回在另一个函数中返回的承诺

我有两个功能。第一个是在到达一条路线时。此路线应创建付款链接并将预订保存到数据库

exports.create_booking = function(req, res) {

      req.body.payment = exports.create_booking_payment(req.body.total_amount);

      console.log(req.body.payment); // This returns ' Promise { <pending> } '

      var new_booking = new Booking(req.body);
      new_booking.save(function(err, booking) {
        if (err)
          res.send(err);
        res.json(booking);
      });

};

我希望有人能在这里帮助我…

您似乎忽略了
异步
函数仍然返回承诺,而不是实际值。因此,当您调用
create\u booking\u payment()
,您将得到一个承诺,您需要使用
。然后()
,或
等待
。没有免费的午餐跨越功能边界<代码>等待允许您在函数内以类似同步的方式编程,但仍然不允许您从函数返回值。当看起来像是从
async
函数返回值时,实际上是返回解析为该值的承诺

因此,您可以使用
async
wait

exports.create_booking = async function(req, res) {

   try{
      req.body.payment = await exports.create_booking_payment(req.body.total_amount);

      console.log(req.body.payment);

      var new_booking = new Booking(req.body);
      new_booking.save(function(err, booking) {
        if (err)
          res.status(500).send(err);
        else 
          res.json(booking);
      });
   } catch(e) {
      res.status(500).send(err);
   } 
};
或者使用
。然后()

注意,我还为这两种场景添加了更完整的错误处理。此外,如果您为
.save()
方法“promisified”或使用已经promisified的接口,则此代码将更干净。我非常不喜欢在基于承诺的代码中使用普通回调异步代码,因为它最终会重复错误处理(如本例所示)


另外,
create\u booking\u payment()
不需要是
async
或使用
wait
,因为您需要它做的只是返回您已经知道如何做的承诺:

exports.create_booking_payment = function() {

  return new Promise (function(resolve, reject) {

      mollie.payments.create({
          amount:      20.00,
          description: "Reservation code: ",
          redirectUrl: "https://www.website.com/",
          webhookUrl:  ""
        }, function(payment) {
              if (payment.error) reject(payment.error)
              else { resolve({
                  id: payment.id,
                  link: payment.getPaymentUrl(),
                  status: payment.status
                })
              }
      });

   });

}

您似乎忽略了
async
函数仍然返回承诺,而不是实际值。因此,当您调用
create\u booking\u payment()
,您将得到一个承诺,您需要使用
。然后()
,或
等待
。没有免费的午餐跨越功能边界<代码>等待允许您在函数内以类似同步的方式编程,但仍然不允许您从函数返回值。当看起来像是从
async
函数返回值时,实际上是返回解析为该值的承诺

因此,您可以使用
async
wait

exports.create_booking = async function(req, res) {

   try{
      req.body.payment = await exports.create_booking_payment(req.body.total_amount);

      console.log(req.body.payment);

      var new_booking = new Booking(req.body);
      new_booking.save(function(err, booking) {
        if (err)
          res.status(500).send(err);
        else 
          res.json(booking);
      });
   } catch(e) {
      res.status(500).send(err);
   } 
};
或者使用
。然后()

注意,我还为这两种场景添加了更完整的错误处理。此外,如果您为
.save()
方法“promisified”或使用已经promisified的接口,则此代码将更干净。我非常不喜欢在基于承诺的代码中使用普通回调异步代码,因为它最终会重复错误处理(如本例所示)


另外,
create\u booking\u payment()
不需要是
async
或使用
wait
,因为您需要它做的只是返回您已经知道如何做的承诺:

exports.create_booking_payment = function() {

  return new Promise (function(resolve, reject) {

      mollie.payments.create({
          amount:      20.00,
          description: "Reservation code: ",
          redirectUrl: "https://www.website.com/",
          webhookUrl:  ""
        }, function(payment) {
              if (payment.error) reject(payment.error)
              else { resolve({
                  id: payment.id,
                  link: payment.getPaymentUrl(),
                  status: payment.status
                })
              }
      });

   });

}

有了这个函数,
导出.create\u booking\u payment
函数不需要有
async
,返回也不需要
等待
,因为它返回的是promise@Kaddath-是的,那是真的,我加上了这个,函数
导出。create_booking_payment
函数不需要具有
async
,并且返回不需要等待,因为它返回的是promise@Kaddath-是的,那是真的,我补充道。