当前位置: 首页 > 知识库问答 >
问题:

等待foreach内部可观察到的几个RXJ,直到完成执行另一个RXJ

翟修永
2023-03-14
  • 我想循环使用选中的\u复选框值
  • 对于每个人,我想做一个POST请求
  • 然后,当foreach中的所有POST请求都完成时,我想得到刚刚查询过的数据

问题:

  • 我的get函数与我的post函数是分开的,所以post查询没有完成,它做get,所以结果是空的get,因为post还没有发布。

html" target="_blank">解决方案:

将观测值添加到数组中,然后将它们放在foreach末尾的forkJoin中。

我所看到的可能:

  • 将observable转换为promise,然后使用async await,对此不太适应

我的API服务返回RxJs可观测值,我在Angular 8中,这里有两个函数:首先是AddVacation(),它将发布一些数据,然后getAgentsFinishDispo()将发布数据。

addVacation() {
    let counter = 0;
    const shift_id = this.selectedShiftForMaincouranteModify;
    const shift_date = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
    this.api.sendGetRequest("/api/shift_dates/" + shift_date, true, null, null)
        .subscribe((data) => {
            this.agents_dispo_checked.forEach((agent) => {
                const agent_id = agent.id;
                if (data) {
                    this.api.sendPostRequest('/api/shift_dos', true, null,
                        { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id })
                        .subscribe();
                } else {
                    this.api.sendPostRequest("/api/shift_dates", true, null, { date: shift_date })
                        .subscribe((data3) => {
                            if (data3.error === "L'association existe deja dans la base de données") {
                                this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id })
                                    .subscribe();
                            } else {
                                this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: data3.date, agent_id: agent_id })
                                    .subscribe();
                            }
                        });
                }
                counter++;
            });
            if (this.agents_dispo_checked.length === counter) {
                this.isOpenSaisieVacation = false;
                this.getAgentsInShiftAndDispo();
            }
        },
    (err) => console.error(err));
}

getAgentsInShiftAndDispo() {
    this.agentsInShift = [];
    this.agents_dispo = [];
    this.agentsInShiftSelectedFormArray.clear();
    this.agentsDispoFormArray.clear();
    if (this.selectedShiftForMaincouranteModify !== 0 &&
        (this.modifyForm.controls.dateDeb.value !== "" || this.modifyForm.controls.dateDeb.value !== null || this.modifyForm.controls.dateDeb.value !== undefined)) {
        const shift_id = this.selectedShiftForMaincouranteModify;
        const goodFormatDate = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
        this.api.sendGetRequest('/api/shift_dos/byShiftAndDate/' + shift_id + "/" + goodFormatDate, true, null, null)
            .subscribe((data) => {
                if (data) {
                    data.forEach((item) => {
                        this.agentsInShift.push(item.agent);
                    });
                }
            }, (err) => console.error(err),
            () => {
                this.agentsInShift.map((o, i) => {
                    const control = new FormControl(true); // if first item set to true, else false
                    this.agentsInShiftSelectedFormArray.push(control);
                });
                const difference = this.agentsMaintenance.filter((obj) => {
                    return !this.agentsInShift.some((obj2) => {
                        return obj.id === obj2.id;
                    });
                });
                this.agents_dispo = difference;
                this.agents_dispo.map((o, i) => {
                    const control = new FormControl(false); // if first item set to true, else false
                    this.agentsDispoFormArray.push(control);
                });
            });
    }
}

提前感谢您在RxJs运营商的大世界中指导我。

共有1个答案

常业
2023-03-14

您必须将所有可观察对象推送到一个数组中,并使用forkJoin。

private observables = [];

addVacation() {
    let counter = 0;
    const shift_id = this.selectedShiftForMaincouranteModify;
    const shift_date = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
    this.api.sendGetRequest("/api/shift_dates/" + shift_date, true, null, null)
        .subscribe((data) => {
            this.agents_dispo_checked.forEach((agent) => {
                const agent_id = agent.id;
                if (data) {
                    this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                        { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id }));
                } else {
                    this.observables.push(this.api.sendPostRequest("/api/shift_dates", true, null, { date: shift_date })
                        .subscribe((data3) => {
                            if (data3.error === "L'association existe deja dans la base de données") {
                                this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: shift_date, agent_id: agent_id }));
                            } else {
                                this.observables.push(this.api.sendPostRequest('/api/shift_dos', true, null,
                                    { shift_id: shift_id, shift_date: data3.date, agent_id: agent_id }));
                            }
                        });
                }
                counter++;
            });
            if (this.agents_dispo_checked.length === counter) {
                this.isOpenSaisieVacation = false;
                forkJoin(this.observables)
                  .subscribe(val => this.getAgentsInShiftAndDispo());
            }
        },
    (err) => console.error(err));
}

getAgentsInShiftAndDispo() {
    this.agentsInShift = [];
    this.agents_dispo = [];
    this.agentsInShiftSelectedFormArray.clear();
    this.agentsDispoFormArray.clear();
    if (this.selectedShiftForMaincouranteModify !== 0 &&
        (this.modifyForm.controls.dateDeb.value !== "" || this.modifyForm.controls.dateDeb.value !== null || this.modifyForm.controls.dateDeb.value !== undefined)) {
        const shift_id = this.selectedShiftForMaincouranteModify;
        const goodFormatDate = this.modifyForm.value.dateDeb.format('YYYY-MM-DD');
        this.api.sendGetRequest('/api/shift_dos/byShiftAndDate/' + shift_id + "/" + goodFormatDate, true, null, null)
            .subscribe((data) => {
                if (data) {
                    data.forEach((item) => {
                        this.agentsInShift.push(item.agent);
                    });
                }
            }, (err) => console.error(err),
            () => {
                this.agentsInShift.map((o, i) => {
                    const control = new FormControl(true); // if first item set to true, else false
                    this.agentsInShiftSelectedFormArray.push(control);
                });
                const difference = this.agentsMaintenance.filter((obj) => {
                    return !this.agentsInShift.some((obj2) => {
                        return obj.id === obj2.id;
                    });
                });
                this.agents_dispo = difference;
                this.agents_dispo.map((o, i) => {
                    const control = new FormControl(false); // if first item set to true, else false
                    this.agentsDispoFormArray.push(control);
                });
            });
    }
}
 类似资料:
  • 问题是 我有一个活动,它定期从API获取数据并显示收到的数据。API 使用 OAuth,因此我会收到一个临时访问令牌,该令牌在一段时间(1 小时)后过期。如果应用尝试使用过期的令牌获取数据,则显然请求将失败。在我的应用的早期迭代中,我对网络请求使用 AsyncTasks,基本上只是执行了一个新的异步任务,该任务将在调用从服务器获取数据的主异步任务之前获取新的访问令牌。这工作得很好,因为主要的Asy

  • 我有这个问题,我一直在寻找,但找不到解决方案(或者也许我不能根据其他答案做出解决方案)。 我的问题是,我需要找到一种方法来等待可观察的(有自己的订户)并等待另一个可观察的(有自己的订户)完成。 场景是这样的: 奥布1- 奥布斯2 - 我主要担心的是我需要两个订阅者。在我看来,obs1 和 obs2 并行运行,但需要检查 obs1 是否以新的会话令牌完成。也许这不是RxJava的主要目的。 Obs1

  • 在一个服务中,我有两个API调用,每个调用都返回一个可观察的,在我的组件中,我有一些条件,如果为true,我必须调用这两个函数,但我需要等待get()调用,这样我就可以使用get调用返回的参数执行post函数。如果为false,我只想用已经定义的参数调用post函数。 服务: 组成部分: 我不想重复帖子调用的代码,或者如果不可能的话,只是不要在另一个订阅()中使用订阅()。我怎么能这么做?没有异步

  • 问题内容: 我在正常( 非ajax )函数中遇到了问题,每个函数中都包含许多 动画 。目前,我只是具有一个between函数,但这并不是完美的,因为没有浏览器/计算机是相同的。 附加说明:它们都有碰撞的单独动画/等。 我不能简单地将一个放在另一个的回调函数中 无论如何在js / jQuery中有: 我知道&,但是这些是针对AJAX的… 我更新的解决方案 jQuery有一个名为$ .timers的暴

  • 问题内容: 如何让我的代码等待,直到DispatchQueue中的任务完成?是否需要任何CompletionHandler或其他东西? 我正在使用Xcode 8.2并在Swift 3中编写。 问题答案: 使用s可以实现这一点。您可以在群组和通话达到平衡时得到通知: 或者您可以等待: 注意 :阻止当前队列(在您的情况下可能是主队列),因此您必须在另一个队列上(如上面的示例代码中)以避免 死锁 。

  • 我想哭我的日程安排,直到我的任务完成。如果有时间执行第二个计划,则必须等到上一个任务尚未完成。我在java启动应用程序中使用@Schedule。我想每5分钟向数据库中插入一次数据,但我想保留我的时间表,直到插入的数据还没有完成,还有时间进行第二次执行。演示代码