简体   繁体   中英

Handling response object from Each promise in Promise.all()

I have an post end point which creates customers and returns userId as response

For example

 // 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) }) }) 

But I need to send a response object back with including userIds

sample response object should be like

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

So over all I would like to have properties of customers and append the created userId to post data object and send as a response.

Plase let me know the best way to do. I like to do asynchronous reqs to create customers

Just push the objects that already have an id to the array. (Or promises for them). Promise.all will then result in the array that you are looking for. The array doesn't necessarily need to contain only promises for the API requests:

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(…));
  }
});

You can also simplify to

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

First you need to filter for users without IDs:

const idLess = customers.filter(customer => !customer.userId);

Then you make your requests for idLess instead of 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
});

Now you need to filter for users with IDs:

const idd = customers.filter(customer => customer.userId);

Then you join your arrays.

Promise.all(arrayPromises).then(result => {
   // either with concat
   res.status(200).json(result.data.concat(idd));

   // or by destructuring
   res.status(200).json([...result.data, ...idd]);
})

You can map the customers , returning the ones that already have a userId and making post requests for those that do not.

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};
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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