0

我正在尝试使用 Bluebird Promises 构建和返回一个对象。Promise 是一个 HTTP 请求,它获取要添加到对象的附加数据。

我创建了一个在 for 循环中执行请求的函数(我也在使用一个执行一些中间件的框架 - 这就是它的意义z.所在)

const getWebAppCustomFieldDetails  = (z, url) => {
    const responsePromise = z.request({
        url:url,
        headers:{
          'content-type': 'application/json'
        }
     });
    return responsePromise
     .then(response =>{
      return JSON.parse(response.content);
     });
};

此函数在以下代码中调用:

const webAppFields  = (z, bundle) => {
//This section carries creates an initial request which gets the bulk of the data
const responsePromise = z.request({
  url: webAppUrl(bundle) + '/' + encodeURI(bundle.inputData.webApp),
  headers:{
    'content-type': 'application/json'
  },
});
//This is an array to hold the objects created from the response
var fields = [];
return responsePromise
  .then(response => {

    response = JSON.parse(response.content);

    //From the response, append the core fields
    response.systemFields.forEach( function (systemField) {
      fields.push({
        'key': systemField.name,
        'required': systemField.required,
        'type': systemField.type.toLowerCase()
      });
    });
    return response;
  })  
  .then(response => {
    //Sometimes there are custom fields that need to be retrieved individually
    const customFieldCount = response.fields.length;
    var customFieldAppend = '';
    for (var i = 0; i < customFieldCount; i++){

      getWebAppCustomFieldDetails(z, response.fields[0].links[0].uri)
        .then(response =>{
          customFieldAppend = {
            'key': response.name,
            'required': response.required,
            'type': response.type.toLowerCase()
          };
          //This push doesn't updated the fields array!
          fields.push(customFieldAppend);
        });
    }
  //This return does not include the custom fields!
  return fields;
  });
};

我不知道如何从嵌套的 Promise 返回值

4

1 回答 1

0

您可以使用Promise.reducefor将您在循环内创建的 Promise 列表减少为单个 Promise:

...
    .then(response => {
        const customFieldCount = response.fields.length;
        var customFieldAppend = '';

        // Promice.reduce will return the accumulator "totalFields"
        return Promise.reduce(response.fields, (totalFields, field) => {
            getWebAppCustomFieldDetails(z, field.links[0].uri) // Using the "field" variable provided by the reducer function
                            .then(response => {
                                customFieldAppend = {
                                    'key': response.name,
                                    'required': response.required,
                                    'type': response.type.toLowerCase()
                                };
                                // Add the data to the accumulator "totalFields"
                                totalFields.push(customFieldAppend);
                            });
        }, []); // The third argument is the initial value of the accummulator. In your example it is a an array, so the initial value is an empty array.
    });
...

Promise.reduce输入三个参数:在 ( response.fields) 上循环的参数列表、reducer函数和累加器的初始值。累加器(我称之为totalFields)是reducer函数的第一个参数,它是用于将列表中的值减少为单个值的变量。在您的情况下,累加器是一个数组(fields您的示例中使用的数组),因此它的初始值是一个空数组。

reduce函数内部,您可以访问列表的单个元素(第二个参数),您可以调用异步操作并在每一步填充累加器广告。该Promise.reduce函数将返回包装在 Promise 中的累加器。出于这个原因,您的函数可以直接返回Promise.reduce的返回承诺。

于 2017-08-02T09:47:53.427 回答