1

I am trying to figure out this one weird issue with dealing with this promise. I have done my fair share of promises so I am not new to it, but with this one function, it is really weird.

const getTrades = () => {
    const accountId = accounts.find(account => account.type === 'INTERNAL').id;
    const response = showTrades({ accountId: accountId }).then(res => console.log(res))
    if (response.length === 0) return false;
    return true;
  };

So the console log of the response above yields an empty array, which is expected. So if the array is empty, it should return false, otherwise it should return true. However, when I call the function like this, it just returns a Promise object.

getTrades()

Anyone know what could be causing this and how to solve this? Thanks!

Kimbo
  • 191
  • 1
  • 10
  • 1
    Issue 1: The function as shown in the snippet cannot return a `Promise`. The most plausible scenario is that the function is actually marked `async` in your code, and you left that bit out in the snippet. Issue 2: `showTrades` likely returns a Promise (assuming based on the `.then()`), so `response.length` tries to look for a property on that Promise, not the actual value inside it. `res` is only available inside the callback passed to `.then()`. – Lucas S. Nov 22 '22 at 16:01
  • Does this answer your question? [Async function returning before await is complete](https://stackoverflow.com/questions/52971118/async-function-returning-before-await-is-complete) – Asraf Nov 22 '22 at 16:08

0 Answers0