Aurelia - 将来自两个 API 调用的 json 合并为一个

Aurelia - Combine json from two API calls into one

我有一个 Aurelia 网络应用程序,可以按名称搜索艺术家,然后 return搜索该艺术家的所有艺术作品。由艺术家搜索得到的 json return 类似于:

{
"source": "Museum",
"language": "EN",
"resultsCount": 1,
"objects": [
  {
  "objectNumber": "125.1988",
  "objectID": 981,
  "title": "Governor's Palace (Raj Bhaven) project, Chandigarh, India",
  "displayName": "Le Corbusier (Charles-Édouard Jeanneret)",
  "alphaSort": "Le Corbusier (Charles-Édouard Jeanneret)",
  "artistID": 3426,
  "displayDate": "French, born Switzerland. 1887–1965",
  "dated": "1951–1976",
  "dateBegin": 1951,
  "dateEnd": 1976,
  "medium": "Wood, cardboard, and plexiglass",
  "dimensions": "33 x 71 1/4 x 65\" (83.8 x 181 x 165.1 cm)",
  "department": "Architecture & Design",
  "classification": "A&D Architectural Model",
  "onView": 0,
  "provenance": "",

  etc....

完整的 json 在这里:

https://github.com/smoore4moma/tms-api/blob/master/object.json

我遇到的问题是,艺术家搜索可以 return 多个名字相似的艺术家("Serra" 例如 returns Richard Serra 和 Daniel Serra-Badué)。所以我可以循环搜索并调用 API 两次来获取艺术作品,但我还没有找到一种方法来组合这两个不同的 API 调用,反正 Aurelia 也没有。这是我正在尝试做的代码片段。我试过 jQuery、.extend、.concat、.push、创建数组、字典....我只是卡住了。

    getByConstituentId(id) {
    return this.http.fetch(baseUrl + "/artists/" + id + token)
    .then(response => response.json())
    .then(response => {
        return response.objects;
    });
    }

// This is not the actual looping code.  But the idea is to combine these two function calls into one result and return that.

     getBySearchTerms(searchCreator) {

     let obj0 = this.getByConstituentId(5350); 
     let obj1 = this.getByConstituentId(5349); 

     return ???  // This is where I am looking for help.
                 // just for ex. "return this.getByConstituentId(5349);" 
                 // works just fine

     }

关于如何做到这一点有什么建议吗?否则,我将构建一个采用艺术家 ID 数组的 API 方法,但我宁愿使用简单的 API 方法并在需要时组合 json。谢谢。

跟进

按照@Tomalak 的建议,这最终奏效了:

   return Promise.all([
   this.getByConstituentId(5349),
   this.getByConstituentId(5350)])
   .then(response => {

     return response[0].concat(response[1]);

    });

参见Promise.all() function

[...] returns a promise that resolves when all of the promises in the iterable argument have resolved, or rejects with the reason of the first passed promise that rejects.

function getByConstituentId(id) {
    return this.http.fetch(baseUrl + "/artists/" + id + token)
    .then(response => response.json().objects);
};

function getBySearchTerms(searchCreator) {
    return Promise.all([
        this.getByConstituentId(5350),
        this.getByConstituentId(5349)
    ]);
}

如果 jQuery 实际上执行了您的 Ajax 调用(即如果 this.http.fetch$.ajax 的包装器),您可以使用 jQuery 的 jquery.when(), which fulfills the same purpose but makes you independent of the browser's native support for the Promise API.

function getBySearchTerms(searchCreator) {
    return $.when(
        this.getByConstituentId(5350),
        this.getByConstituentId(5349)
    );
}

注意不同的调用约定。 $.when 需要一个参数列表,而 Promise.all() 需要一个可迭代对象(如数组)。

您可以通过 .apply()$.when 与数组参数一起使用。

function getBySearchTerms(searchCreator) {
    return $.when.apply($, [
        this.getByConstituentId(5350),
        this.getByConstituentId(5349)
    ]);
}