M
M
mgmtx2022-02-02 18:39:17
JavaScript
mgmtx, 2022-02-02 18:39:17

Why do Promises work weird?

Good afternoon!

There is such a design:

const getData = async (data) => {
  data = JSON.parse(data);

  const promises = await data.bloggers.map((item) => getInfo(response.access_token, item.twitch_nickname));

  return Promise.all(promises);
};
getData(data).then(data => console.log(data));

async function getInfo(token, name) {
  const response = await axios.get(`https://api.twitch.tv/helix/streams?user_login=${name}`, {
    headers: {
      'Client-Id': '1uv0rg1z4x',
      'Authorization': `Bearer ${token}`
    },
  });

  let isLive = response.data.data.length != 0 ? true : false;
  return isLive;
}


In this form, promises work well, give the result, but if you add curly braces to .map(), the construction stops working. Values ​​are returned undefined. getInfo returns a Promise { pending } like this:

...
const promises = await data.bloggers.map((item) =>  {
  getInfo(response.access_token, item.twitch_nickname);
});
...


Can someone explain why and what to do about it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2022-02-02
@Seasle

returntried to write?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question