-2

Been on this for over 24 hours, everything seems to work as I want but the promise keeps returning null.

[
    null,
    null
]

here are my codes:

  let vettedBatch = currentBatch.Items.map((current) => {
    getUser(current.userId).then((res) => {
      // return resolve(JSON.parse(res.body));
      let body = JSON.parse(res.body);
      if (body.hasOwnProperty("Item")) {
        return body;
      } else {
        //if user does not exist on the users table based on ID, lets take his transaction out of the fail-safe table
        console.log(
          `user with id number ${current.userId} with transaction id ${current.txId} do not exist or must have been disabled`
        );
        User.deleteWithdrawalTx(current.txId).then(() => {
          console.log(
            `transaction id ${current.txId} delete for unknown user with userId ${current.userId}`
          );
        });
      }
    });
  });
Acetech
  • 398
  • 4
  • 16
  • 2
    You are mixing syntaxes in a bad way. Also you dont return anything in `currentBatch.Items.map`, thats why it is null – MauriceNino Oct 05 '20 at 13:34
  • `getUser(current.userId)` returns a promise, so no need of creating a new promise and returning something from the callback function of `.then()` method won't make it a return value of the callback function of `.map()`. – Yousaf Oct 05 '20 at 13:34

2 Answers2

0

Instead of mixing async/await and Promise syntax, I would suggest you to stick to one.

Here would be your code written fully in async/await syntax:

const getData = async () => { // async function instead of new Promise()...
    return Promise.all(currentBatch.Items.map(async (current) => { // make map async and await it with Promise.all()

        const res = await getUser(current.userId); // await instead of .then()

        let body = JSON.parse(res.body);
        if (body.hasOwnProperty("Item")) {
            return body;
        } else {
            console.log(`user with id number ${current.userId} with transaction id ${current.txId} do not exist or must have been disabled`);
            await User.deleteWithdrawalTx(current.txId); // await instead of .then()
            console.log(`transaction id ${current.txId} delete for unknown user with userId ${current.userId}`);

            // You should return something here too, but I dont know what you want to return, so...
        }
    }));
}

let vettedBatch = await getData(); // await the async function

Your problem is actually a deviation of this question: How do I return the response from an asynchronous call?. Should be fixed in my answer, but I still suggest you to check out the linked thread.

MauriceNino
  • 6,214
  • 1
  • 23
  • 60
  • It didn't work "magically". Please read my comments and check out the linked answer. @Acetech Also if it solved your problem, accept my answer – MauriceNino Oct 05 '20 at 13:44
0

You need to use Promise.all:

const data = [];
const promises = currentBatch.Items.map(async current => {
    return await getUser(current.userId)
  });

Promise.all(promises)
.then(res => {
  res.map(item => {
   let { body } = JSON.parse(item);
   if (body.hasOwnProperty("Item")) {
      data.push(body);
    } else {
       console.log('message');
    }
})
})
V.Tur
  • 1,115
  • 9
  • 20
  • 2
    `const promises = currentBatch.Items.map(current => getUser(current.userId));` is sufficient – Thomas Oct 05 '20 at 14:27
  • This is done in a very bad way. Promise.all returns an array of all results, so you don't have to mix scopes. Your data[] is practically unusable outside the Promise.all().then() call, because it will not be initialized until the then runs. This is an unexplained bad answer to the problem. – MauriceNino Oct 06 '20 at 08:22