6

i have a question about using async await inside another promise. I have a function call another function to get a transaction details.

When i running the function LastTransactions the field details do not show results. Anyone can help me ?

LastTransactions: async (transactionKey, page) => {

    const api = `https://api.pagar.me/1/payables?recipient_id=${transactionKey}&count=${totalResults}&page=${page}&api_key=${PagarmeApiKey}`;
    const response = await axios.get(api);

    transactions = response.data.map((item) => {

      return {
        id : item.id,
        transactionId : item.transaction_id,
        trxDetails : [transactionDetails(item.transaction_id)],
      }

    });

    return transactions;

  },

and a detail function

async function transactionDetails(id){
    const response = await axios.get(`https://api.pagar.me/1/transactions/${id}?api_key=${PagarmeApiKey}`)
    const data = response.data;
    return data;
}
1
  • transactionDetails returns a promise. Map an async function over your array so you can await calls to it. const transactionPromises = response.data.map(async item =>...); then return await Promise.all(transactionPromises) Commented Jul 19, 2020 at 7:17

2 Answers 2

7

You need to utilize the Promise.all method to take an array of promises and return an array with your transactions once each individual call for transaction details finishes.

async (transactionKey, page) => {
  const api = 
    `https://api.pagar.me/1/payables?recipient_id=${transactionKey}&count=${totalResults}&page=${page}&api_key=${PagarmeApiKey}`;
  const response = await axios.get(api);
  
  // create an array of promises and wait for
  // all of them to resolve before continuing
  const transactions = await Promise.all(
    response.data.map(async item => {
      const { id, transaction_id } = item;

      // get transaction details for each item in the array
      const trxDetails = await transactionDetails(transaction_id);

      return {
        id,
        trxDetails,
        transactionId: transaction_id,
      };
    })
  );
  return transactions;
};

References:

Sign up to request clarification or add additional context in comments.

Comments

0

Since transactionDetails(item.transaction_id) is Asynchronous, you need to await that as well, otherwise it will return immediately and trxDetails will contain a promise object, and not response.data.

try this:

transactions = response.data.map(async (item) => {

      return {
        id : item.id,
        transactionId : item.transaction_id,
        trxDetails : [await transactionDetails(item.transaction_id)],
      }

    });

2 Comments

i try o use the two options but i receive a blank array like this. I look the functions transactionsDetails to check if data API is OK and i can print the data but i cant set the results in details : array ``` [ {}, {}, {}, {}, {}, {}, {}, {}, {}, {} ] ```
Which object are you printing which gives you this array of empty objects?

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.