如何使可观察的/承诺等待其他诺言首先发生?

发布于 2025-02-09 02:44:55 字数 962 浏览 2 评论 0 原文

我在Angular中具有NRGX效果,在那里我推动纸张以promisse.lass.sine ande。

但是,一个合格需要花费一些时间来运行,因为它从服务器中获取数据,但是问题在于纸张。所有这些都在一个特定的奖励结束之前运行,所以我没有得到所需的所有结果。

我的问题是如何做出尊重。

代码:

effect = this.actions$.ofType(projectAction.GETDATA).
    .switchMap(([action,state])) => {
     
    // this is the function that needs to be completed before the Promisses.all
    this.getNameData().then( data => {
      promisses.push( this.dataService.getAllNameData(data))
    })
   
    names.forEach((name) => {
       promisses.push( this.dataService.getAllNameData(name))
    })

    return Observable.fromPromisse(
      Promisse.all(promisses).then((results) => {
            //data logic
              return new dataAction.getDataSucess(data)
        })
    );
}


// function: 
getNameData() {
  return Promise.resolve().then(() => this.getNameFromDatabase() // this returns data from the server)
} 

我希望我的解释很清楚。我有小时尝试为此找到解决方案

I have a nrgx effect in angular, where i am pushing promisses to then resolve them with a Promisse.all.

But one promisse takes some time to run because its fetching data from the server, but the problem is that the Promisses.all runs before that one particular promisse ends so i dont get all the results that i need.

My question is how to make so that the Promisse.all waits first for my big time promisse?

Code:

effect = this.actions$.ofType(projectAction.GETDATA).
    .switchMap(([action,state])) => {
     
    // this is the function that needs to be completed before the Promisses.all
    this.getNameData().then( data => {
      promisses.push( this.dataService.getAllNameData(data))
    })
   
    names.forEach((name) => {
       promisses.push( this.dataService.getAllNameData(name))
    })

    return Observable.fromPromisse(
      Promisse.all(promisses).then((results) => {
            //data logic
              return new dataAction.getDataSucess(data)
        })
    );
}


// function: 
getNameData() {
  return Promise.resolve().then(() => this.getNameFromDatabase() // this returns data from the server)
} 

I hope my explanation was clear. I am with hours trying to find a solution for this

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

骄傲 2025-02-16 02:44:55

您可以通过 fork join 实现这一目标。
将每个诺言包裹在a 并放入全球叉。
定义按定义加入叉子等待所有请求已完成

You can achieve that by fork join .
Wrap each promise in a from and put in a global fork.
Fork Join by definition waits for all requests to be completed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文