8
8
83349 M2018-11-29 15:45:21
JavaScript
83349 M, 2018-11-29 15:45:21

How to wait for the execution of the function, and then execute the condition?

There are 2 functions checkNicknames() and ckechNikname(). Function

export const checkNicknames = (nicknames) => {
  let result = [];
  nicknames.map( nickname => {
    if (nickname) {
      let st = ckechNikname(nickname);
      st && result.push(st)
    }
  });
  return result;
};
From this function, the data is transferred to the .jsx file and entered into the state there.
The problem is in the ckechNikname function. In theory, this function gives the data to the above specified function. It does the following
const ckechNikname = (nickname) => {
  let isParam = true;
  let params = {};

  getAccountId(nickname).then( async (data) => {
    let account_id = data.data.data[0].account_id;
    await getStatistic1(account_id).then(data => {
      //обработка полученных данных по первому запросу
    });
    await getStatistic2(account_id).then(data => {
      //обработка полученных данных по второму запросу 
    });
  });
  if (isParam) { return params} else { return false; }
};
getAccountId, getStatistic1, getStatistic2 - requests to the server. All data is displayed correctly when outputting to the console, but when passed in the condition, as I understand it, they do not have time to be entered into variables like isParam and params by this moment. How to make sure that all data is transferred?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
RAX7, 2018-11-29
@f1o007

you getAccountId(nickname).then( async (data) =>{....}) is asynchronously executed, so ckechNikname will complete before something gets into the isParam and params variables.
https://codepen.io/anon/pen/NEEXwW?editors=0010

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question