Hey there!
I've been tinkering with an Express project lately and I'm having a bit of trouble grasping the ins and outs of async functions and promises. Here's how my project is structured:
-controllers --userController.js -services --paymentService.js
The payment service handles integration with a third-party payment tool, and it defines a method that looks like this:
const pay = async(data) => {
service.pay()
.then(function(response){
return response;
}).catch(function(err)){
console.log(err);
});
}
I'm trying to call this method from my userController:
let payUser = async(req, res) => {
const response = await paymentService.pay(req.data);
if(response) {
res.send(response)
}
}
However, the response always ends up being undefined. It doesn't seem to wait as expected because of the "await" keyword.
How exactly does this work? What am I missing if I want to ensure it waits until the response is returned?