-1
function testData() {
  return new Promise((resolve, reject) => {
    Data.findAll({
      where: {
        adress: "New York",
      },
      attributes: [
        "id", "name", "type"
      ]
    }).then((object) => {

      let data1 = [],
        data2 = [];
      object.map(async (value) => {
        data1.push([value.name, value.type]);
      });
      console.log("data1 Value", data1);

      let test;
      object.map(async (value) => {
        test = await getValue(value.name);
        data2.push([value.name, value.type, test]);
      });
      console.log("data1 Value", data2);
    }).catch((err) => {
      return reject(err);
    });
  });
}

So I have wrote a code to fetch data from database and I am saving it in object

Here below for example getting this data from database so my Object contain this value

let object = [{
  id: `01`, name: `fish`, type: `marine`,
}, {
  id: `02`, name: `fish`, type: `fresh`,
}, {
  id: `03`, name: `fish`, type: `tank`,
}, {
  id: `04`, name: `animal`, type: `pet`,
}]
    
let data1=[],data2=[];

object.map(async (value) => {
  data1.push([value.name,value.type]);            
})
console.log("data1 Value",data1);

let test;
object.map(async (value) => {
 test = await getValue(value.name);
  data2.push([value.name,value.type,test]);            
})
console.log("data1 Value",data2);

As you can see I am writing async and await inside map function and consoling data outside map function and its not working and where as in data1 I am getting the value but inside data2 I am getting blank value.

I am not sure what should I do to get test value inside my data2

Here getValue() is lengthy code that's why I can post but its working code as i can console it's value.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Aakash
  • 139
  • 1
  • 3
  • 22
  • [map](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) already returns an array, pushing into a new one is not necessary. You can just return the value. This returned array holds all your promises which you can then pass to something like [Promise.all()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) – Reyno Jul 20 '21 at 11:18
  • 1. Don't use `.map()` for simple array iteration. Use `.forEach()` or an actual loop. 2. `async`/`await` cannot work with `.map()`/`.forEach()` the way you've done it. You need to use `Promise.all()` and *return the values or use a regular loop if the sequence is relevant. – VLAZ Jul 20 '21 at 11:20
  • 1
    Unrelated to your question but don't declare `test` outside the scope it's used in. – 404 Jul 20 '21 at 11:20
  • `promise.all()` where should i write it @VLAZ – Aakash Jul 20 '21 at 11:23
  • @Aakash check the duplicates. It'd be `Promise.all(object.map(value => getValue(value.name)))`. It still returns a promise that will resolve to the array of all values. – VLAZ Jul 20 '21 at 11:26
  • Not exactly related but the promise in your `testData()` function is never resolved, so it will be pending forever. The bigger problem is [using the explicit promise constructor anti-pattern](https://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). Since `Data.findAll` returns a promise, there is no reason to wrap it in another promise. – VLAZ Jul 20 '21 at 11:29

1 Answers1

0

First of all why are you using map for this ?? map returns a new array. You should use forEach/for loop for this.

second you should not use async things in map array methods as far as I am aware. remove the async/await inside of the array method. so now you get an array of unresolved promises

after the loop is over use Promise.all([...data2]) to resolve them

Swagath Shetty
  • 153
  • 1
  • 2
  • 9