0

I have this getUser function that wait for 1 sec before it is running.

I created an async function findUser that takes array of from getuser after awaiting to be completed. However, I keep receiving promise{} in console message.

function getUsers() {
  let users = [];

  // delay 1 second (1000ms)
  setTimeout(() => {
    users = [
      { username: 'john', email: 'john@test.com' },
      { username: 'jane', email: 'jane@test.com' },
    ];
  }, 1000);

  return users;
}

async function findUser(username) {
  const users = await getUsers();
  const user = users.find((name)=>{
      console.log(username);
      return name.username === username;
  })
  console.log(user)
  return user;
}


findUser('john');


George Go
  • 73
  • 1
  • 9
  • 1
    setTimeout does not return a promise. Debug your code (attach your IDE). You will see that `users` will be returned before it is assigned and that the `await getUsers` will return an empty array because the `await` is discarded as there is no promise. Once you learn how to debug your code and inspect values and step through you will be able to troubleshoot issues much faster. That should be the next thing you do. – Igor Jul 06 '22 at 19:07
  • 1
    getUsers() is not an async function, so you can't use await on it. – justanotherguy Jul 06 '22 at 19:07
  • 1
    You cant just use `await` and put it infront of anything and expect that it will do what you want. – bill.gates Jul 06 '22 at 19:13
  • @justanotherguy You need to use `await` on a promise (technically, on anything, but it's useless on non-thenables). Whether `getUsers` is marked as an `async` function doesn't matter though. – Bergi Jul 06 '22 at 19:36
  • @Bergi Really? That's interesting – justanotherguy Jul 07 '22 at 01:05

1 Answers1

0

You getUsers functions isn't returning a promise, you can't await it.

Wrap the setTimeout into the promise so you can return that and wait for it.

function getUsers() {
  return new Promise((resolve) => {

    // delay 1 second (1000ms)
    setTimeout(() => {
      resolve([{
          username: 'john',
          email: 'john@test.com'
        },
        {
          username: 'jane',
          email: 'jane@test.com'
        },
      ]);
    }, 1000);
  })
}

async function findUser(username) {
  const users = await getUsers();
  const user = users.find((name) => {
    console.log(username);
    return name.username === username;
  })
  console.log(user)
  return user;
}


findUser('john');

To resolve discussion in the comments: If you want to get the value out of findUser('john'); you have to either await it or use .then(). Examples:

const user = await findUser('john');
…; // do something with user here

or

findUser('john').then(user => {
  …; // do something with user here
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Konrad
  • 21,590
  • 4
  • 28
  • 64
  • The last line needs to be `await findUser('john')` – Barmar Jul 06 '22 at 19:10
  • @Barmar you can' have `await` in the global scope. And it doesn't have to be awaited as promises run anyway. But in a different example, it would be helpful to wait for it or use `.then` to get the value. – Konrad Jul 06 '22 at 19:11
  • I thought `await` was also allowed at top-level. He wants to see the returned value in the console, not the `Promise{}` object. – Barmar Jul 06 '22 at 19:12
  • 1
    the returned value IS in the condole. Didn't you try it? And i just discovered they can't be top level myself in something i was doing. – John Lord Jul 06 '22 at 19:12
  • @JohnLord value printed in the console is quite useless if you want to do anything else with it. – VLAZ Jul 06 '22 at 19:14
  • so assign it to a variable instead. – John Lord Jul 06 '22 at 19:15
  • @JohnLord and to do that, you'd use an `await`. Well done, we've come full circle. – VLAZ Jul 06 '22 at 19:16
  • 1
    i was referring to declaring a variable outside the scope of the async function and assigning to it instead of printing to the console. You wouldn't need an await or a then. You would simply have to check for undefined. Not super-useful in this case though i'll admit. – John Lord Jul 06 '22 at 19:25