【问题标题】:How would you appropriately use do a GET request that relies on other requests? [duplicate]您将如何正确使用依赖于其他请求的 GET 请求? [复制]
【发布时间】:2019-06-11 17:48:26
【问题描述】:

我正在尝试正确执行一些异步调用。我基本上有这样的东西,依次是getData1,然后是getData2,最后是userCheck

for (const item of someArray) {
  this.getData1(item, userID).then(() =>
    this.getData2(item, userID).then(() =>
      this.userCheck(item)
    )
  );
}

然后在getData1getData2 中订阅我创建的服务中的GET 请求。我有一个功能正常工作,因为它只是一个简单的 Get 请求。我在使用其他功能时遇到问题。它基本上是三个GET 请求,其中第二个调用依赖于第一个,第三个调用依赖于第二个。我已经合并了承诺和then 声明来尝试实现这一目标,但它仍然无法正常工作。我不工作的那个我尝试了几种不同的方法,但都没有运气。我的功能目前看起来像这样。

getData2(user: string, id: string)
    {
        return new Promise((resolve, reject) =>
        {
            var status: string;

            this._apiService.getAssertion(id).toPromise().then((data: any) =>
            {
                let assert = data.toString();
                console.log(assert);
                this._apiService.getToken(assert, user).toPromise().then((data: any) =>
                {
                    let tkn = data.access_token.toString();
                    console.log(tkn);
                    this._apiService.ccMeta(tkn, guid).toPromise().then((information: any) =>
                    {
                        parseString(information, (err, result) =>
                        {
                            if (err)
                            {
                                throw new Error(err);
                            }
                            else
                            {
                                status = result['entry']
                                this.ccData.push(
                                {
                                    key: 'userStatus',
                                    value: status
                                })
                            }
                        });
                    });
                });
            }).then(() =>
                resolve()).then();
        })

【问题讨论】:

  • 您需要将第二个 http 调用放在第一个 response 可用的 then 回调中。
  • @Bergi 你能检查我的编辑吗
  • 在更新的代码中,您需要避免使用Promise constructor antipattern,并且您可能正在寻找flatten your promise chain。不要忘记来自then 回调的return 值(或承诺)!
  • @Bergi:好的,我现在了解扁平化和反模式。因此,我试图将第一个 _api.service 调用的结果传播到第二个调用,因为第二个调用需要从第一个返回的值,但事实证明我在第二个调用中遇到错误,好像它不是正确传播。我不确定出了什么问题,因为我正在遵循link 中的建议。
  • 在您发布的代码中,第一个请求的结果需要作为第二个请求的参数,但在第二个的回调中不需要 - 链接的问题(和its duplicate)不需要似乎在这里适用。你只需要一个扁平链,类似于apiService.getAssertion(id).then(assert => { return apiService.getToken(assert.toString(), user); }).then(data => { return apiService.ccMeta(data.access_token.toString(), guid); }).then(parseStringPromise).then(information => { this.ccData.push({key: 'userStatus', value: information.entry}); });

标签: angular asynchronous promise


【解决方案1】:

你可以使用一些 Rxjs 来代替 Promise。

import { concatMap } from 'rxjs/operators';

this.http.post(someURL, httpOptions1).pipe(
   concatMap(res => {
      return this.http.get('second/req/url');
   }),
   concatMap(resFromSecondReq => {
      return this.http.get('third/req/url');
   })
).subscribe(resFromThirdReq => console.log('All requests successsful'));

【讨论】:

  • 这里 rxjs 如何比 promise 更有优势?而这个答案并没有解释和解决问题中代码中的根本问题。
  • 为什么将 observables 转换为 Promise 来做一些可以使用 observables 来完成的事情,并且代码看起来更干净,并且在需要时仍然保留使用更多 rxjs 运算符的潜力。此示例应执行您在对原始答案的评论中建议的相同操作。
猜你喜欢
  • 2014-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-25
相关资源
最近更新 更多