伪代码
doStuff() {
// A is an asynchronous call
A
.then(result_a => { // process result_a })
.then(result_b => { // process result_b })
.then(() => { // side_effects})
}
如何返回result_b
?
背景
我正在使用TypeORM将订单和订单行保存到sqlite数据库中,用于angular + electron桌面应用程序。我认为这不是特定于Angular的。除了以非常线性的方式链接它们之外,我对使用then
不太熟悉。
问题陈述
如何返回then链中间的purchase_order
?我正在寻找这个或purchase_order.id
。
简化版本:
addOrder(header: PurchaseOrder, lines: PurchaseOrderLine[]) {
const new_order = new PurchaseOrder();
this.databaseService.connection
.then(conn => conn.manager.save(new_order))
.then(purchase_order => {
let new_lines = lines.map(line => {
const new_order_line = new PurchaseOrderLine();
this.databaseService
.connection
.then(conn => conn.manager.save(new_order_line));
})
})
.then(() => this.reflectChanges());
}
完整版:
addOrder(header: PurchaseOrder, lines: PurchaseOrderLine[]) {
const new_order = new PurchaseOrder();
// copy header details to new order
new_order.purchase_order_date = (<any>header.purchase_order_date).format();
new_order.total_value = header.total_value;
if(header.delivery_required_by) {
new_order.delivery_required_by = (<any>header.delivery_required_by).format();
}
new_order.notes = header.notes;
new_order.supplier = _state.selectedSupplier;
// use databaseService to save order and then save the order lines
this.databaseService
.connection
.then(conn => conn.manager.save(new_order))
.then(purchase_order => {
let new_lines = lines.map(line => {
const new_order_line = new PurchaseOrderLine();
new_order_line.quantity = line.quantity
new_order_line.code = line.code
new_order_line.file_no = line.file_no
new_order_line.description = line.description
new_order_line.unit = line.unit
new_order_line.rate = line.rate
new_order_line.total = line.total
new_order_line.note = line.note
new_order_line.purchase_order = purchase_order
this.databaseService
.connection
.then(conn => conn.manager.save(new_order_line));
})
})
.then(() => this.reflectChanges());
}