简体   繁体   English

获取对象数组Promise,然后遍历对象

[英]Get object array promise, then iterate over objects

I am trying to create an array called pw_array, assign the contents of pw_subscribers to that array, then append each object in the pw_array with new key value pairs from the second promise. 我正在尝试创建一个名为pw_array的数组,将pw_subscribers的内容分配给该数组,然后将pw_array中的每个对象附加来自第二个promise的新键值对。 I am new to promises and having a lot of trouble making this work. 我是新来的诺言,在完成这项工作时遇到很多麻烦。 Right now when I console.log(pw_customer) inside the second promise, inside the getCustomers function, it is returning what I want. 现在,当我在getProfers函数内的第二个Promise内console.log(pw_customer)时,它正在返回我想要的东西。 But when I console.log(pw_array) later it is the original array. 但是,当我稍后console.log(pw_array)时,它是原始数组。

var pw_array = [];
//first promise is working correctly
var getPaywhirlSubscribers = new Promise(function(resolve, reject) {

paywhirl.Subscriptions.getsubscribers({limit:100}, function(error, pw_subscribers) {
        Promise.all(JSON.parse(pw_subscribers).forEach(function(pw_subscriber) {
             pw_array.push(pw_subscriber);
        }))
        // console.log(pw_array);
        return resolve(pw_array);
    });
});

var getGatewayReferences = function(pw_array) {
    return new Promise(function(resolve, reject) {
        Promise.all(pw_array.forEach(function(pw_customer) {
            paywhirl.Customers.getCustomer(pw_customer.customer_id, function(error, customer) {
                pw_customer.phone = customer.phone;
                pw_customer.address = customer.address;
                pw_customer.gateway_reference = customer.gateway_reference;
                // this console.log is returning what I want
                // console.log(pw_customer);
            }); 
        }));
        resolve(pw_array);
        // console.log(pw_array);
    });
};

and the promise chain... 和承诺链...

getPaywhirlSubscribers.then(getGatewayReferences).then(function(pw_array) {
  // this console.log is returning the original pw_array with pw_subscribers but not with the appended pw_customer keys
  console.log(pw_array);
});

All of your code can be reduced to 您所有的代码都可以简化为

var getPaywhirlSubscribers = function() {
  return new Promise(function(res, rej) {
    paywhirl.Subscriptions.getSubscribers({limit:100}, function(err, subs) {
      if (err) {
        rej(err);
      } else {
        res(JSON.parse(subs));
      }
    });
  });
};

var gatewayRefs = function(promiseOfArray) {
  return promiseOfArray.then(function(subs) {
    return Promise.all(subs.map(function(sub) {
      return new Promise(function(res, rej) {
        paywhirl.Customers.getCustomer(sub.customer_id, function(err, customer) {
          if (err) {
            rej(err);
          } else {
            res(Object.assign({}, sub, customer);
          }
        });
      });
    });
  });
};

gatewayRefs(getPaywhirlSubscribers()).then(function(arrayOfCustomers) {
  // do something with the customer array
});

Note that you can make this even shorter/simpler if you use one of the many utilities available to automatically convert node.js-style error -first callback APIs into Promise-based ones. 请注意,如果您使用许多可用的实用程序之一来自动将node.js样式的error-first回调API转换为基于Promise的API,则可以使其更短/更简单。 Search for 'promise denodeify'. 搜索“ promise denodeify”。

You could also conceivably pull some of the steps out into a .then chain to reduce nesting, but that's as much aesthetic as practical IMHO. 您也可以想象将某些步骤拉到.then链中以减少嵌套,但这与实际的IMHO一样美观。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM