Closed fancytboy closed 7 years ago
That's javascript programming for you mate.
What exactly do you want to do?
Save the result of the function for later use. e.g when i Initialize a transaction, i need to extract the payment URL and send to the customer so he/she can make payments.
@fancytboy You should learn to use promises. Look at a library like Q. You could build your request like this:
var q = require('q');
function initTransaction(ref, amnt, email) {
var defer = q.defer();
paystack.transaction.initialize({
reference: ref,
amount: amnt,
email: email
}, (error, body) =>{
if (error) {
defer.reject(error);
} else {
defer.resolve(body);
}
});
return defer.promise;
}
Now you can call that initTransaction() function anywhere. E.g:
initTransaction("123445566", 10000, "email@gmail.com").then((body) =>{
// Do whatever you'd normally do with a successfully initiated transaction;
console.log(body);
}).catch((error) => {
// Handle error
console.log(error);
});
This issue is more appropriate as a stackoverflow question by the way.
Actually, i had already resolved the issue using callbacks. I was just being lazy since I have to carry out about 5 different validations (all Asynchronous) before generating a payment URL. That could really be rough and tedious. Just thought there could be a better way. I just have to embrace the Asynchronous nature of Javascript. Thanks anyways. @verygreenboi
@fancytboy glad your issue is solved. Its more a javascript thing. Thanks
Hi, i've been using your api and i noticed the functions are asynchronous and cannot return a value. They can only be displayed in the console log. e.g
paystack.transaction.initialize({ reference: "123445566" , amount: 10000, email: "email@gmail.com" } , function(error,body){ if(!error){ console.log(body) }else{ console.log(error) } })
the body cannot be returned so as to extract the redirect URL.