gpt4 book ai didi

javascript - 使用来自多个异步调用的值,然后执行计算以推送值

转载 作者:行者123 更新时间:2023-12-01 23:19:13 25 4
gpt4 key购买 nike

我正在进行 4 个异步 api 调用

floor.entities.forEach(elementId => {
let objTemp: any = {};

objTemp.main = elementId.name

let currentTempForZone;

//1st async function
this.getCurrentValue(objTemp.main).subscribe((curr) =>
{
objTemp.currentTemp = curr.rows[0].val
}
);
//2nd async function
this.getDesiredHeatingTemp(elementId._id).subscribe(([des]) =>
{
objTemp.desiredTempHeating = des.rows[0].val.split(':')[1]
}
);
//3rd async function
this.getDesiredCoolingTemp(elementId._id).subscribe(([des]) =>
{
objTemp.desiredTempCooling = des.rows[0].val.split(':')[1]
}
);

let condition;

//4th
this.deviceHelper.getConditioningStatusForZone(elementId._id);
this.deviceHelper.conditioningTypeSubject.subscribe(condType => {
console.log(condType);
condition = condType
});

if(condition == 'first' ){
let zoneTempDiff1 = objTemp.currentTemp - objTemp.desiredTempHeating;
let tempColor1 = this.customColors.filter(color => zoneTempDiff1 < (color.temp + 1) && zoneTempDiff1 > (color.temp - 1));
objTemp.tempColorToInsert = tempColor1.color
}
else if(condition == 'second' ){
let zoneTempDiff2 = objTemp.currentTemp - objTemp.desiredTempCooling;
let tempColor2 = this.customColors.filter(color => zoneTempDiff2 < (color.temp + 1) && zoneTempDiff2 > (color.temp - 1));
objTemp.tempColorToInsert = tempColor2.color
}

floor.droppeditem.push(objTemp);
}

我得到了所有 3 个值 objTemp.currentTemp、objTemp.desiredTempHeating、objTemp.desiredTempCooling 和条件,但它们都是异步的在使用上述 4 个值进行计算后,如何为 objTemp.tempColorToInsert 赋值。

============================================= =============================

customColors: any = [
{
color: '#50B3D3',
temp: -1
},
{
color: '#25CBE4',
temp: -2
},
{
color: '#25CBE4',
temp: 0
},
{
color: '#7EE2DD',
temp: 1
},
{
color: '#7EE2DD',
temp: 2
}
]

更新

我做了类似的事情并且正在获取值,只是想知道响应会自动映射到它们各自的元素

   forkJoin([this.getCurrentValue(objTemp.main),this.getDesiredHeatingTemp(elementId._id),this.getDesiredCoolingTemp(elementId._id)])
.subscribe(([current,[desiredheat], [desirecooling]])=>{
objTemp.currentTemp = current.rows[0].val;
objTemp.desiredTempHeating = desiredheat.rows[0].val.split(':')[1];
objTemp.desiredTempCooling = desirecooling.rows[0].val.split(':')[1];

let condition = 'first'

if(condition == 'first' ){
let zoneTempDiff1 = objTemp.currentTemp - objTemp.desiredTempHeating;
let tempColor1 = this.temperatureColors.filter(color => zoneTempDiff1 < (color.temp + 1) && zoneTempDiff1 > (color.temp - 1));
objTemp.tempColorToInsert = tempColor1.color
}else if(condition == 'second' ){
let zoneTempDiff2 = objTemp.currentTemp - objTemp.desiredTempCooling;
let tempColor2 = this.temperatureColors.filter(color => zoneTempDiff2 < (color.temp + 1) && zoneTempDiff2 > (color.temp - 1));
objTemp.tempColorToInsert = tempColor2.color
}
})

最佳答案

您可以使用 forkJoin

const all$ = [
this.getCurrentValue(objTemp.main),
this.getDesiredHeatingTemp(elementId._id),
this.getDesiredCoolingTemp(elementId._id),
this.deviceHelper.getConditioningStatusForZone(elementId._id)
]

forkJoin(all$).subscribe(response => {
objTemp.currentTemp = response[0].rows[0].val;
objTemp.desiredTempHeating = response[1][0].rows[0].val.split(':')[1];
objTemp.desiredTempCooling = response[2][0].rows[0].val.split(':')[1];
condition = response[3];
// now while you have all 4 values
// Do your thing here
})

关于javascript - 使用来自多个异步调用的值,然后执行计算以推送值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58199431/

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