0

Now I have an array with four promises like this:

fieldsForSave:  [ Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> } ]

Now I just can get each one of four

fieldsForSave.map(async (promise) => {
        let result = await promise;
        console.log(result);//for example result is { field: 5b1c4558337f87fa32aec69c, isRequired: true }
      });

How can I get the resolves like this, I want to use this array to update mongoose schema.

fieldsForSave:  [ { field: 5b1c4558337f87fa32aec69b, isRequired: true }
{ field: 5b1c4558337f87fa32aec69c, isRequired: true }
{ field: 5b1c4558337f87fa32aec69d, isRequired: true }
{ field: 5b1c4558337f87fa32aec69e, isRequired: true } ]
shakell
  • 351
  • 1
  • 2
  • 10
  • Any specific reason why you want without promise? promise.all. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all – Priyesh Kumar Jun 10 '18 at 10:22

1 Answers1

0

You can use Promise.all in order to get one promise out of multiple promises:

let results = await Promise.all(fieldsForSave);
console.log(results);
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504