Reputation: 8465
I been trying to make work Meteor.WrapAsync
I have read Meteor wrapAsync syntax answer, this video https://www.eventedmind.com/feed/meteor-meteor-wrapasync and I just cant figure how to return
the response from the call from Stripe. I'm using console.log
to print the steps, and I been reaching throw number 4 which mean, Im reaching stripe
server and getting the response, but after that I can't see why console.log(5)
its not printing. please if somebody can help me to understand why its the wrapAsyn its not returning the stripe callback?
//this functions are part of an anonymous function and running in the server side of meteor
stripe.charge = function (stripeToken) {
// get a sync version of our API async func
var strypeChargeSync = Meteor.wrapAsync(stripe.charge.process);
// call the sync version of our API func with the parameters from the method call
console.log("1");
var response = strypeChargeSync(stripeToken);
console.log("5" + response); ///// this never get print / log
return response;
}
stripe.charge.process = function(stripeToken){
var _stripe = StripeAPI(stripeKey);
console.log("2");
var charge = _stripe.charges.create({
amount: 1000, // amount in cents, again
currency: "cad",
card: stripeToken.id,
description: "paid@whatever"
}, function(err, charge) {
if (err && err.type === 'StripeCardError') {
alert("Sorry we couldn't charge the money: " + err);
//console.log(err);
}else{
console.log("4");
//console.log(charge);
return charge;
}
});
console.log("3");
}
//current output 1,2,3,4 but never 5 :(
EDIT
this is how I end having the Stripe function thanks for the support
var syncFunction = Meteor.wrapAsync(_stripe.charges.create, _stripe.charges);
var response = syncFunction({
amount: 1000, // amount in cents, again
currency: "cad",
card: stripeToken.id,
description: "paid@whatever"
});
Upvotes: 2
Views: 1719
Reputation: 22696
You are wrapping the wrong function here, Meteor.wrapAsync
transforms an async function (this means a function which transmits its result to the caller via a callback) in a synchronous one.
The function you pass to Meteor.wrapAsync
does not have a callback as final argument, you should instead wrap _stripe.charge.create
.
stripe.charge = function (stripeToken) {
var _stripe = StripeAPI(stripeToken);
var stripeChargeSync = Meteor.wrapAsync(_stripe.charge.create,_.stripe.charge);
var response = stripeChargeSync({
amount: 1000, // amount in cents, again
currency: "cad",
card: stripeToken.id,
description: "paid@whatever"
});
return response;
};
If you want to handle errors, you should use a try
/catch
block when calling stripe.charge
.
try{
stripe.charge(STRIPE_TOKEN);
}
catch(exception){
console.log("Sorry we couldn't charge the money",exception);
}
I see you are logging your error using alert
, are you trying to use Meteor.wrapAsync
on the client ? Meteor.wrapAsync
is meant to be used on the server because the environment needed to provide synchronous-looking execution is available in Node.js, not the browser.
Upvotes: 4