简体   繁体   English

Angular:如何等待请求响应,然后在 function 中进行下一步

[英]Angular: how to wait for the request response and after that proceed next step in function

I have a little problem with my function, one of the params that I want to set I'm getting from the http request.我的 function 有一点问题,我想设置的参数之一是从 http 请求中获得的。 The problem is that the final data from method below is recalculated in one of the components, and when it happens the result of request is still null.问题是下面方法的最终数据在其中一个组件中重新计算,当它发生时,请求的结果仍然是 null。 When the response come it's not triggering onChanges so I can't recalculate this data again, and doCheck triggering "too often".当响应到来时,它不会触发onChanges ,因此我无法再次重新计算此数据,并且doCheck触发“过于频繁”。

updateData(componentRecords: ComponentRecord[], importSourceGroup?: ImportSource[], isImportSource = false, component: DiaryNode = null) {
    const recordData = [];
    const records = isImportSource ? importSourceGroup : componentRecords;
    for (const record of records) {
      const recordRow: any = record.ID === 'addingRow' ? record : {
        ID: record.ID,
        InputTypeID: record.InputTypeID,
        SubRecords: record.SubRecords,
        attachmentField: record.Fields ? record.Fields.find(({Type}) => Type === DiaryFieldType.ATTACHMENT) : null,
        documentsFolder: null,
        DateUpdated: null,
        ComponentInstanceID: null,
        linkedUnits: {},
        recordRef: record
      };

      if (record.ID !== 'addingRow') {

        if (isImportSource) {
          recordRow.DateUpdated = (record as ImportSource).DateUpdated;
          recordRow.ComponentInstanceID = (record as ImportSource).ComponentInstanceID;
        }

        if (recordRow.attachmentField && recordRow.attachmentField.Value) {
          this.subManager.add(this.documentsApiService
            .getFileDetailsByFolderID(recordRow.attachmentField.Value)
            .subscribe((documents: DocumentsFolder) =>
              recordRow.documentsFolder = documents));
        }

        if (record.Fields) {
          for (const field of record.Fields) {
            const label = field.Label;
            recordRow[label] = field.Type === DiaryFieldType.INTEGER ? parseInt(field.Value, 10) : field.Value;
            const schema = component && component.Schema && component.Schema.find(diaryFormField => diaryFormField.Label === label);
            if (schema && schema.LinkedUnit) {
              recordRow.linkedUnits[label] = schema.LinkedUnit.Attributes.List.PickListItems[0].Label;
            }
          }
        }
      }
      recordData.push(recordRow);
    }
    return recordData;
  }

The part that is async is异步的部分是

        if (recordRow.attachmentField && recordRow.attachmentField.Value) {
          this.subManager.add(this.documentsApiService
            .getFileDetailsByFolderID(recordRow.attachmentField.Value)
            .subscribe((documents: DocumentsFolder) =>
              recordRow.documentsFolder = documents));
        }

So I don't know what is the best solution for this but I was wondering if it's possible to wait here for the response, and go furthure when it comes.所以我不知道什么是最好的解决方案,但我想知道是否可以在这里等待响应,以及 go 何时到来。

What do you think?你怎么看?

In short the property cannot be assigned synchronously using the asynchronous HTTP request.简而言之,无法使用异步 HTTP 请求同步分配属性。 Instead you need to make entire paradigm asynchronous.相反,您需要使整个范例异步。 Then you could subscribe to the function updateData() to fetch the array.然后你可以订阅 function updateData()来获取数组。

Additionally you could use RxJS forkJoin function to combine multiple parallel observables.此外,您可以使用 RxJS forkJoin function 组合多个并行可观察对象。 Try the following尝试以下

updateData(
  componentRecords: ComponentRecord[], 
  importSourceGroup?: ImportSource[], 
  isImportSource = false, 
  component: DiaryNode = null
): Observable<any> {                      // <-- return `Observable` here
  const records = isImportSource ? importSourceGroup : componentRecords;

  return forkJoin(                        // <-- use `forkJoin` to combine multiple parallel observables
    records.map(record => {
      const recordRow: any = record.ID === 'addingRow' ? record : {
        ID: record.ID,
        InputTypeID: record.InputTypeID,
        SubRecords: record.SubRecords,
        attachmentField: record.Fields ? record.Fields.find(({Type}) => Type === DiaryFieldType.ATTACHMENT) : null,
        documentsFolder: null,
        DateUpdated: null,
        ComponentInstanceID: null,
        linkedUnits: {},
        recordRef: record
      };

      if (record.ID !== 'addingRow') {
        if (isImportSource) {
          recordRow.DateUpdated = (record as ImportSource).DateUpdated;
          recordRow.ComponentInstanceID = (record as ImportSource).ComponentInstanceID;
        }

        if (record.Fields) {
          for (const field of record.Fields) {
            const label = field.Label;
            recordRow[label] = field.Type === DiaryFieldType.INTEGER ? parseInt(field.Value, 10) : field.Value;
            const schema = component && component.Schema && component.Schema.find(diaryFormField => diaryFormField.Label === label);
            if (schema && schema.LinkedUnit) {
              recordRow.linkedUnits[label] = schema.LinkedUnit.Attributes.List.PickListItems[0].Label;
            }
          }
        }

        if (recordRow.attachmentField && recordRow.attachmentField.Value) {
          return this.documentsApiService.getFileDetailsByFolderID(recordRow.attachmentField.Value).pipe(     // <-- return the HTTP request
            map((documents: DocumentsFolder) => ({ ...recordRow, recordRow.documentsFolder: documents }))     // <-- spread operator to append new value to object
          );
        }

        return of(recordRow);       // <-- use `of()` to return as observable
      }

      return of(recordRow);       // <-- use `of()` to return as observable
    })
  );
}

See here to learn more about fetching info from async request.请参阅此处以了解有关从异步请求中获取信息的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在发出下一个请求之前等待响应? - How to wait for a response before making the next request? Angular - 等待函数调用完成,继续执行代码 - Angular - Wait for function call to finish, to proceed with code XMLHttpRequest GET 函数在发送请求后返回。 需要弄清楚如何获取等待响应的函数 - XMLHttpRequest GET function returning after sending the request. Need to figure out how to get function to wait for response 如何在处理下一步之前等待 HTTP 请求在循环中获得响应 - how to wait for HTTP requests to get response in a loop before its processes to next step NodeJS在下一个请求之前等待HTTP响应 - NodeJS wait for HTTP Response before next Request 如何在 angular 8 中使用订阅方法等待 http 请求返回响应? - how to wait for http request for returning response with in subscribe method in angular 8? 循环访问Angular HTTP请求中的对象,请等到响应后再运行下一个循环 - Looping through objects in Angular HTTP request, wait until response before running next loop 如何等待下一个POST请求 - How to wait for the next POST request 仅当功能已运行10次时才继续执行下一步 - Proceed to next step only when function has been run 10 times Javascript-异步功能完成后如何等待下一行执行? - Javascript - How next line wait to be executed after asyncronus function is done?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM