0

some is only working for one object although input1 and 2 can have 500 object. If I put logic inside some() method it's working fine.

How to call method asynchronously inside some. filterRecord method has some logic which is re-usable that's why I want to have this inside separate method . Does some() not work with async await?

const filter1 = [{
  "filter1Key": "age",
  "filter2Key": "score"
}];
const filter2 = [{
  "filter1Key": "name",
  "filter2Key": "address.city"
}];

const test1 = input1.forEach((data) => input2.some(async(obj) => {
  // filter logic is inside filterRecord method
  const output = await filterRecord();
}));

async filterRecord() {
}
<script>
  const input1 = [{
      'name': "name1",
      'email': "email1@email.com",
      'age': 10,
      'score': 95,
      'address': {
        'city': "city1"
      }
    },
    {
      'name': "name2",
      'email': "email2@email.com",
      'age': 10,
      'score': 45,
      'address': {
        'city': "city2"
      }
    },
    {
      'name': "name3",
      'email': "email3@email.com",
      'age': 20,
      'score': 65,
      'address': {
        'city': "city3"
      }
    }
  ];

  // Second Array
  const input2 = [{
      'id': 1,
      'fullname': "name1",
      'emailaddress': "email1@email.com",
      'age': 10,
      'score': 45,
      'address': {
        'city': "city1"
      }
    },
    {
      'id': 5,
      'name': "name2",
      'email': "email2@email.com",
      'age': 20,
      'score': 55,
      'address': {
        'city': "city2"
      }
    },
    {
      'name': "name31",
      'email': "email3@email.com",
      'age': 20,
      'score': 65,
      'address': {
        'city': "city3"
      }
    }
  ];
</script>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
user13000875
  • 387
  • 2
  • 14
  • I made you a snippet. Please edit in into a [mcve] describing the expected output and the reason you want to use await in the first place. There does not seem to be any need here – mplungjan Feb 05 '23 at 07:07
  • I want to use await because that method has lot's of logic and also it's reusable that's why I want to keep this in separate method – user13000875 Feb 05 '23 at 07:32
  • Does this answer your question? [How to store async/await map() result in a variable?](https://stackoverflow.com/questions/73754994/how-to-store-async-await-map-result-in-a-variable) – Heiko Theißen Feb 05 '23 at 07:40
  • It is also unclear to me what you want to achieve, the function inside `input1.forEach(...)` has no effect on `input1`, it just computes a boolean value. – Heiko Theißen Feb 05 '23 at 07:47
  • @HeikoTheißen filter record will push inside an new array inside foreach – user13000875 Feb 05 '23 at 07:51
  • 1
    I cannot find anywhere a usecase for await for non-async javascript. I may not have looked enough, but when not using async, I see not point in using await – mplungjan Feb 05 '23 at 08:52
  • Does this answer your question? [Using async/await with a forEach loop](https://stackoverflow.com/questions/37576685/using-async-await-with-a-foreach-loop) – pilchard Feb 05 '23 at 10:45
  • I flagged a duplicate explaining why async doesn't work, but I agree with mplungjan, it seems you are confused about what async is. – pilchard Feb 05 '23 at 10:47
  • @pilchard I am not confused I know async await very well . I want to call method inside some and for that I have use await . I have mentioned same in question as well – user13000875 Feb 05 '23 at 15:17
  • If you know async very well then you will immediately understand why it won't work in `some()` even if `some()` was able to respect an async callback. An async function ***always returns a Promise*** which will always be truthy in the eyes of `some()` and so will never get past the first element of the array. `[1, 1, 1].some(async (e) => e === 3);` -> `true` – pilchard Feb 05 '23 at 15:34

0 Answers0