0

I have the following code:

var arrayAxios = [];
            
const array = async() => {
  await sleep(3000);
  _.forEach(tasks, task => {
    let res = _.includes("selected", task.value);
    if (res) {
      this.axios
        .get(url, { params: { data1 } })
        .then(response => {
          arrayAxios.push(this.axios.post(url, { dataAAA }));
        });
    }
  });
};
              
var promises = async() => {
  const arrayAxios = await array();
  await Promise.all(arrayAxios)
    .then(response => {
      resetAll_dataAAA();   // <--- my dataAAA in arrayAxios are reset before post in Promise.all!
    })
    .catch(error => {
      console.log(error);
    });
};

promises();

The function "resetAll_data()" is executed before the data is post into the DDBB. I can't find the problem.

Any suggestions?

Thank you very much!

3
  • 1
    What's the point of the sleep(3000) call? Commented Mar 9, 2019 at 13:35
  • 1
    arrayAxios (the one inside promises) is undefined because array doesn't return anything Commented Mar 9, 2019 at 13:35
  • you're not returning the promise so it wont wait Commented Mar 9, 2019 at 13:38

2 Answers 2

1

You are looking for

async function array() {
  await sleep(3000);
  var arrayAxios = []; // declare the array locally
  _.forEach(tasks, task => {
    let res = _.includes("selected", task.value);
    if (res) {
      arrayAxios.push(this.axios
//    ^^^^^^^^^^^^^^^ push to the array immediately, not asynchronously
        .get(url, { params: { data1 } })
        .then(response => {
          return this.axios.post(url, { dataAAA }));
//        ^^^^^^ chain the promises
        })
      );
    }
  });
  return arrayAxios;
//^^^^^^ give the result to the caller
}
Sign up to request clarification or add additional context in comments.

Comments

0

I've simplified your example so you can hopefully get what's going on :)

You need to return an array of promises from an array to be able to resolve them with Promise.all

there's no point using await Promise.all().then use await or then

array was never returning the request promise so when awaited it would of resolved straight away

sleep i dont believe is a function in node also.

any questions let me know happy to help.

const axios = {
  get: async(task) => {
    return task.res;
  },
  post: async(task) => {
    return task;
  }
}

const tasks = [{url: 'getting', res: {got: 'got the thing', post: 'thing to post'}}, {url: 'getting 2', res: {got: 'got the thing 2', post: 'thing to post 2'}}]

const getRequest = async(url = '') => { 
  const res = await axios.get(url)
  console.log(res.got)
  return postRequest(res.post)
}

const postRequest = async (url = '') => {
  const res = await axios.post(url)
  console.log(res)
  return res;
}

const array = async() => {
  const createRequests = async task => {
    return getRequest(task)
  }

  return tasks.map(createRequests).filter(Boolean);
};

const promises = async() => {
  console.log('waiting')
  const res = await array()
  const all = await Promise.all(res)
  console.log('awaited')
  console.log('can call your reset')
};

promises();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.