1

I am just trying to find out what in the world is going on here when trying to resolve the promise. Like my code below, I have been trying to take care and resolve the promise but it is just being a pain at this point even after searching for the answers all over.

const getTrades = async () => {
    const accountId = accounts && accounts.find(account => account.type === 'EXCHANGE').id;

    await getTradesInfo({ accountId: quadAccountId });
  };

  const pending = getTrades().then(res => { return res });

  const resolved = pending.then(res => { return res });

  console.log(resolved);

So for some reason, the resolved variable above is still showing a pending object.

E_net4
  • 27,810
  • 13
  • 101
  • 139
Kimbo
  • 191
  • 1
  • 10
  • 1
    Missing `return` keyword in your `getTrades` function? – Tim Klein Nov 22 '22 at 20:58
  • To return the value from the "then"... you need to use await... otherwise you will get the promise – Pipe Nov 22 '22 at 20:59
  • What I can get through this code is: the `then` returns a promise object. If you really want to use `pending`, better to use `const pending = await getTrades().then(res => { return res });` – Rohit Khanna Nov 22 '22 at 21:00
  • `then` does not return the result of the callback - it can't, the callback is invoked later! - but it returns a promise for the result. – Bergi Nov 23 '22 at 02:46

1 Answers1

0

your code is still asynchronous, your console log won't wait for your promise to be executed.

here a possible solution:

const getTrades = async () => {
    const accountId = accounts && accounts.find(account => account.type === 'EXCHANGE').id;

   return getTradesInfo({ accountId: quadAccountId });
  };

  getTrades.then((res)=> <here can use your console.log> )

or wrapping it with async/await:

const getTrades = async () => {
  const accountId = accounts && accounts.find(account => account.type === 'EXCHANGE').id;

 return getTradesInfo({ accountId: quadAccountId });
};
(async ()=> {
  const result  = await getTrades();

  console.log(result)
})()
Martinez
  • 1,109
  • 1
  • 4
  • 13