wrapAsync correct result caught as an error

I am trying to integrate a third party payment gateway and have to use wrapAsync to wait for the response before redirecting customers to their external page. For some reason the correct response gets caught as an error.

The code below is only executed on the server. And the result will be that I get the payment in the catch statement. So console output:

–> ‘error’
–> payment object

If I do not use try / catch I get a 500 Internal error of undefined

var convertCreatePaymentsToSync  = Meteor.wrapAsync( mollie.payments.create, mollie.payments );
        try {
          var result = convertCreatePaymentsToSync({
            amount:      10.00,
            description: "My first API payment",
            redirectUrl: "someurl"
          });
          console.log('success');
          return result;
        } catch (err) {
           console.log('error');
            return err;
        }

Am I missing something obvious here?

Thank for your help

I should mention that this implementation using Future works correctly

Meteor.methods({
  createPayment(){

    future = new Future();

    const paymentObject = {
      amount:      10.00,
      description: "My first API payment",
      redirectUrl: "someurl"
    }

    mollie.payments.create(paymentObject, function(payment){
      future.return(payment.getPaymentUrl());
    })

    return future.wait();
  }
})

The problem is that Meteor.wrapAsync expects the function to have a very specific signature for its callback, specifically (error, result) => {}. According to the Mollie docs, it just calls the callback with a single payment object:

mollie.payments.create({
    amount:      10.00,
    description: "My first API payment",
    redirectUrl: "https://webshop.example.org/order/12345/"
},  function (payment) {
    response.writeHead(302, { Location: payment.getPaymentUrl() })
});

They should probably improve their Node.js library to follow the (error, result) pattern.

1 Like