我是javascript的新手,我正在努力进行同步/异步函数调用,尤其是在这里,因为我试图连接同一数据库的两个集合中的数据。
这是我的代码:
class BraintreePaymentProcessFormView(FormView):
template_name = 'startupconfort/cart.html'
success_url = '/'
form_class = BraintreeSaleForm
http_method_names = ['post']
success_url = reverse_lazy('my_shopping_cart')
def get_context_data(self, **kwargs):
# import ipdb; ipdb.set_trace()
context = super().get_context_data(**kwargs)
context['client_token'] = get_braintree_client_token()
return context
def form_valid(self, form):
# import ipdb; ipdb.set_trace()
user = self.request.user
nonce = form.cleaned_data['payment_method_nonce']
result = braintree.Transaction.sale({
"amount": get_total_price_of_the_shipping_cart(user),
"payment_method_nonce": nonce,
"options": {
"submit_for_settlement": True
}
})
reverse_lazy('my_shopping_cart')
# import ipdb; ipdb.set_trace()
if result.is_success or result.transaction:
print(result.transaction)
messages.success(self.request, 'Payment proceed successfully')
# Add Total into db with User Name and Product + Quantity
#send email
user = self.request.user
billing_email = user.shipping_address.billing_email
customer_name = user.shipping_address.customer_name
#total values
#msg
# Email with mailjet..
msg = EmailMultiAlternatives(
subject="Please activate your account",
body="Click to activate your account: http://localhost:8000/",
from_email="Example <admin@example.com>",
to=[
"New User <user1@example.com>"
],
reply_to=["Helpdesk <support@example.com>"]
)
# Send it:
# msg.send()
#Clear Cart
CartItem.objects.filter(customer=user).all().delete()
# return super().form_valid(form)
import ipdb; ipdb.set_trace()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self,form):
# Add action to invalid form phase
messages.success(self.request, 'An error occured while processing the payment')
return self.render_to_response(self.get_context_data(form=form))
并返回function getAcquisitionAreas(req) {
PartAreas.getAcquisitionAreasByAcquisitionId(req.params.id, (err, partArea) => {
particle.areas = partArea._doc.areas;
});
}
function getAcquisitionPerimeters(req) {
PartPerimeters.getAcquisitionPerimetersByAcuiqisiontId(req.params.id, (err, partPerimeter) => {
particle.borders_intern = partPerimeter._doc.borders_intern;
particle.borders_extern = partPerimeter._doc.borders_extern;
});
}
function getParticleData(req, callback) {
getAcquisitionAreas(req);
getAcquisitionPerimeters(req);
callback;
}
function constructParticle(req) {
particle.id = req.params.id;
}
// Register
router.get('/get/:id', (req, res, next) => {
getParticleData(req);
constructParticle(req);
res.send(particle);
});
,这意味着它在放入区域和边框之前执行{"id":"1508515120"}
。我该如何实现呢?我已经看过Promises但是无法使它工作,并且在回调方法中实现回调方法似乎很脏。
谢谢!
res.send(particle)
我终于设法找到了一个有效的解决方案:
var particle;
function getAcquisitionAreas(req) {
return new Promise((resolve, reject) => {
PartAreas.getAcquisitionAreasByAcquisitionId(req.params.id, (err, partArea) => {
if (err) { reject(err); }
particle.areas = partArea._doc.areas;
resolve();
});
});
}
function getAcquisitionPerimeters(req) {
return new Promise((resolve, reject) => {
PartPerimeters.getAcquisitionPerimetersByAcuiqisiontId(req.params.id, (err, partPerimeter) => {
if (err) { reject(err); }
particle.borders_intern = partPerimeter._doc.borders_intern;
particle.borders_extern = partPerimeter._doc.borders_extern;
resolve();
});
})
}
function constructParticle(req) {
return new Promise((resolve, reject) => {
particle.id = req.params.id;
resolve();
})
}
function sendParticle(res) {
res.send(particle);
}
// Register
router.get('/get/:id', (req, res, next) => {
particle = {};
getAcquisitionAreas(req)
.then(getAcquisitionPerimeters(req))
.then(constructParticle(req))
.then(sendParticle(res));
});
答案 0 :(得分:0)
有几种方法可以实现这一目标(Promises,Yield,Async / Await)。你已经尝试过Promises,所以让我们来看看。我已经在这里做了一些假设,并且有更简洁的方法可以做到这一点,但考虑到你发布的代码示例,下面的代码示例应该会给你带来最多,如果不是所有我认为的那样。
function getAcquisitionAreas(req) {
return new Promise((resolve, reject) => {
PartAreas.getAcquisitionAreasByAcquisitionId(req.params.id, (err, partArea) => {
if (err) { return reject(err); }
particle.areas = partArea._doc.areas;
resolve();
});
});
}
function getAcquisitionPerimeters(req) {
return new Promise((resolve, reject) => {
PartPerimeters.getAcquisitionPerimetersByAcuiqisiontId(req.params.id, (err, partPerimeter) => {
if (err) { return reject(err); }
particle.borders_intern = partPerimeter._doc.borders_intern;
particle.borders_extern = partPerimeter._doc.borders_extern;
resolve();
});
})
}
function getParticleData(req, callback) {
getAcquisitionAreas(req)
.then(getAcquisitionPerimeters(req));
.then(callback);
}
function constructParticle(req) {
particle.id = req.params.id;
}
// Register
router.get('/get/:id', (req, res, next) => {
getParticleData(req, () => {
constructParticle(req);
res.send(particle);
});
});
我做的事情就像捕捉承诺被拒绝时抛出的错误一样,对我来说,从承诺切换到中间的回调似乎很奇怪,但希望上面的例子能给你一个基本的想法如何使用它们。