处理Promise.all()中每个诺言的响应对象

时间:2018-10-25 16:42:10

标签: javascript node.js reactjs es6-promise

我有一个后端,它创建客户并返回userId作为响应

例如

// Post Data
const customers = [{
    firstName: 'John',
    lastName: 'Smith',
    userId: '12345
  },
  {
    firstName: 'Phil',
    lastName: 'Doe',
  },
  {
    firstName: 'Dan',
    lastName: 'Joe',
  }
]


app.post('/customers', (req, res) => {
  const arrayPromises = []
  const customers = req.body.customers;
  customers.forEach((customer) => {
    //checking whether I have a userId already
    if (!customer.userId) {
      const postRequest = {
        headers: Object.assign(res._headers, {
          Accept: 'application/json',
          'Content-Type': 'application/json',
        }),
        post:'www.createUserExample.com,
        customer // post data,
      };
      arrayPromises.push(axios(postRequest))
      //response will be {userId}
    }
  })


  Promise.all(arrayPromises).then(result => {
    //by this way I get all userids
    res.status(200).json(result.data)
  })


})

但是我需要发送一个包含userIds的响应对象

样本响应对象应该像

[{
    firstName: 'John',
    lastName: 'Smith',
    userId: '12345'
  },
  {
    firstName: 'Phil',
    lastName: 'Doe',
    userId: '65765'
  },
  {
    firstName: 'Dan',
    lastName: 'Joe',
    userId: '786876'
  }
]

因此,总的来说,我想拥有客户的属性,并将创建的userId附加到发布数据对象并作为响应发送。

请让我知道最好的方法。我喜欢做异步请求来创建客户

3 个答案:

答案 0 :(得分:1)

只需将已经具有ID的对象推送到数组。 (或对他们的承诺)。 Promise.all将导致您要查找的数组。数组不一定需要仅包含对API请求的承诺:

const arrayPromises = []
const customers = req.body.customers;
customers.forEach((customer) => {
  //checking whether I have a userId already
  if (customer.userId) {
    arrayPromises.push(Promise.resolve(customer));
  } else {
    arrayPromises.push(axios(…));
  }
});

您还可以简化为

const arrayPromises = req.body.customers.map((customer) => {
  //checking whether I have a userId already
  return customer.userId
    ? Promise.resolve(customer)
    : axios(…);
});

答案 1 :(得分:0)

首先,您需要为没有ID的用户过滤:

mManualViewListener

然后,您请求ManualScreen而不是const idLess = customers.filter(customer => !customer.userId);

idLess

现在,您需要为具有 ID ID的用户进行过滤:

customers

然后您加入阵列。

const arrayPromises = idLess.map(customer => {
    const postRequest = {
        headers: Object.assign(res._headers, {
           Accept: 'application/json',
           'Content-Type': 'application/json',
        }),
        post:'www.createUserExample.com,
        customer // post data,
    };

    // if your service responds with the entire obj:
    return axios(postRequest);

    // if your service responds just with ids:
    return axios(postRequest).then(id => Object.assign({
       userId: id
    }, customer))l
});

答案 2 :(得分:0)

您可以映射customers,返回已经有userId的对象,并向没有const promises = customers.map(async (customer) => { //checking whether I have a userId already if (customer.userId) { return customer; } const {userId} = await axios({ headers: Object.assign(res._headers, { Accept: 'application/json', 'Content-Type': 'application/json', }), post: 'www.createUserExample.com', customer // post data, }); return {userId, ...customer}; }); 的对象发出发布请求。

.accordion-container {
  & .collapsed {
  }
}