gpt4 book ai didi

angular - 嵌套函数调用不起作用(代码流问题)

转载 作者:太空狗 更新时间:2023-10-29 19:27:40 27 4
gpt4 key购买 nike

我的代码有问题:

问题是这样的:

如果 param.qtamodificata == false,代码运行正常,但如果 params.qtamodificata == true(并且它调用 getQuantities 函数)它返回“未定义”,因为异步调用...

我需要以同步方式设置代码流,以便仅在所有过程完成处理后才返回结果...

如何解决?

感谢支持

获取订单行

public getOrderRow(params): Observable<any>
{
//OFFLINE SWITCH OK
var res = { headers: [], data: [] }

return Observable.fromPromise(this._WebDBService.sysConfigurazione.toArray().then(
data => {

var art = JSON.parse(data[0].ordinidatajson).filter(
function(itm,index){
return itm.codart.v === params.codart;
}
);

res.headers = JSON.parse(data[0].ordiniheaderjson);

if(params.qtamodificata == true)
{
this.getQuantities(art[0].codart.v,null,params.udmmodificata, params['qtaord'+ params.udmmodificata]).subscribe(
qtys =>{
art[0].qtaord1.v = qtys.dqta1;
art[0].qtaord2.v = qtys.dqta2;
art[0].qtaord3.v = qtys.dqta3;

res.data = art[0];

return res;
}
);
}
else
{
res.data = art[0];
return res;
}
}
));
}

获取数量

public getQuantities(codart: string, codvar: string, idqta: number, qta: number): Observable<any>{

return Observable.fromPromise(this._WebDBService.tbArticoli.where({codart: codart}).toArray().then(
data =>
{
console.log('getQuantities');

var qtys = {
dqta1: 0,
dqta2: 0,
dqta3: 0
}

var a = data[0];

switch (idqta) {
case 1:
qtys.dqta1 = +qta;
if (a.rapudm12 > 0 && a.codudm2)
qtys.dqta2 = qta / a.rapudm12;
if (a.rapudm23 > 0 && a.codudm3)
qtys.dqta3 = qta / a.rapudm23;
break;
case 2:
qtys.dqta2 = +qta;
if (a.rapudm12 > 0 && a.codudm1)
qtys.dqta1 = qta / a.rapudm12;
if (a.rapudm23 > 0 && a.codudm3)
qtys.dqta3 = qta / a.rapudm23;
break;
case 3:
qtys.dqta3 = +qta;
if (a.rapudm23 > 0 && a.codudm2)
qtys.dqta3 = qta / a.rapudm23;
if (a.rapudm12 > 0 && a.codudm1)
qtys.dqta1 = qta / a.rapudm12;
break;
}
return qtys;

}));
}

函数调用

this.orderService.getOrderRow(params, 'test')
.subscribe(list => {
console.log('onlineorder3');
console.log(list);
console.log('onlineorder3');
}

最佳答案

我看到你转换了PromissesObservables ,在这种情况下,我认为 Promises连同async/await将使您的代码更容易正确排序,也更容易阅读和理解。一个版本是:

public async getOrderRow(params): Promise<any> {
//OFFLINE SWITCH OK
var res = { headers: [], data: [] }

var data = await this._WebDBService.sysConfigurazione.toArray();
var art = JSON.parse(data[0].ordinidatajson).filter(
function (itm, index) {
return itm.codart.v === params.codart;
}
);

res.headers = JSON.parse(data[0].ordiniheaderjson);

if (params.qtamodificata == true) {
let qtys = await this.getQuantities(art[0].codart.v, null, params.udmmodificata, params['qtaord' + params.udmmodificata]);

art[0].qtaord1.v = qtys.dqta1;
art[0].qtaord2.v = qtys.dqta2;
art[0].qtaord3.v = qtys.dqta3;

res.data = art[0];

return res;

}
else {
res.data = art[0];
return res;
}
}

public async getQuantities(codart: string, codvar: string, idqta: number, qta: number): Promise<any> {

let data = await this._WebDBService.tbArticoli.where({ codart: codart }).toArray();
console.log('getQuantities');
var qtys = {
dqta1: 0,
dqta2: 0,
dqta3: 0
}
// ... Omitted for brevity
return qtys;
}

注意我用了Promise<any>对于返回类型,我会推荐而不是 any您使用实际描述结果的类型。例如 getQuantities返回值可能应该是 Promise<{dqta1:number, dqta2:number, dqta3:number }> .

关于angular - 嵌套函数调用不起作用(代码流问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47513406/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com