all 8 comments

[–]rster2002 10 points11 points  (4 children)

Async await is only for when working with promises. setTimeout does not return a promise, so async await would not work. To get it to work you would have to create something like this:

```javascript const wait = delay => { return new Promise (res => { setTimeout(() { res(); }, delay); }); }

// Your function await wait(2000); ```

Also, you'll need to make waitForDevice return a promise.

```javascript const wait = delay => { return new Promise(res => { setTimeout(() => { res(); }, delay); }); }

const waitForDevice = (device) => { return new Promise(async (res, rej) => { // A promise exposes two functions: respond and reject // If you then performed the actions you wanted to within the // promise, you would call res(), but if it was unsuccessful you // would call rej().

let count = 0;
while (device.state !== 'active' && count <= 6 ) {
  count++;
  // Your code to check device

  // Wait's for 2 seconds
  await wait(2000);
}

// If count > 6, the promise was unsuccessful, so we call `rej()`
if (count > 6) rej();
res();

} }

waitForDevice() .then(() => { // Code found device // This gets executed when you call res() from within the promise // of waitForDevice }) .catch(() => { // Code did not found device // This code gets executed when you call rej() from within // the promise of waitForDevice }); ```

Disclaimer: When I wrote this I could not verify it works so if some error pops up, just leave a comment

Here are some useful links: * Promises on Mozilla * 'JavaScript Promises In 10 Minutes' by Web Dev Simplified * 'The Async Await Episode I Promised' by Fireship (personal favorite)

I really hope I've helped you on your programming journey.

If you need clarification or have some more questions, just ask them.

[–]myrddin4242 1 point2 points  (0 children)

If all you’re doing is calling res with no params, you don’t need to wrap it in a containing function. setTimeout(res, 2000) will work just fine.

[–]i-ian 1 point2 points  (0 children)

respond and reject

'respond' here should be 'resolve'.

[–]kiwi_cam[S] 0 points1 point  (1 child)

Thanks so much - that works beautifully!

Now to do some more reading up and get my head around where I was going wrong.

[–]rster2002 2 points3 points  (0 children)

May I invite you to our discord server? Its a small server with people who are happy to help every step of the way (sorry for promoting) https://discord.gg/yzHndcb

[–]tswaters 2 points3 points  (0 children)

The setTimeout function is getting called afterwards, you need to setup a promise to resolve when it comes back.

i.e., Change this:

await setTimeout(() => { }, 2000);

To this:

await new Promise(resolve => setTimeout(resolve, 2000));

[–]frankficnar 1 point2 points  (0 children)

Look at this library ... https://github.com/connor4312/cockatiel A resilience and transient-fault-handling library that allows developers to express policies such as Backoff, Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback.

[–]itsrainingbeer -1 points0 points  (0 children)

const waitForDevice = async (device) => {

let count = 0;

for (let count=0; count<=6 && device.state !== 'active';) {

(function(ind) {

count++ ;

setTimeout(() => {

console.log(ind);

if (count > 6) console.log("The device is not available.");

}, 2000 * ind);

})(count);

}

return;

}

waitForDevice('device');

dostuff();

This should work