-1

All examples I see about Asynchronous JavaScript when they use callbacks, promises or async/await with a delay, to fake examples like waiting for something, make use of setTimeout() or setInterval(). What if I don't get in a callback, promise or async/await an allready built-in asynchronous code? I want to fake the delay, and can do it with Date() in a synchronous way.

It's not a question just about how to sleep/delay without using setTimeout() or setInterval(). This is just an example, and like to see asynchronous JavaScript code without invoking a built-in asynchronous method.

My synchronous code is:

const sleep = (milliseconds) => {
    let t0 = Date.now();
    const t1 = Date.now() + milliseconds;
    console.log(`Sleeping ${milliseconds} milliseconds`);
    while (t0 <= t1) {
        t0 = Date.now();
    }
};

const randomMilliseconds = () => {
    return Math.round(Math.random() * 10000);
};

const returnElement = () => {
        sleep(randomMilliseconds());
        console.log(element);
    };

const elements = [1, 2, 3, 4, 5];

function loopElements() {
    for (element of elements) {
        returnElement();
    }
}

loopElements();

I read about async functions and thought I could do this with loopElements() or returnElement(), but wasn't able to do it. Also all examples I see about async looping are with setTimeout() or use other built-in async solutions like fetching data from an API.

Can this be done? And just with a simple Date() example.

7
  • 1
    If you're not invoking a built-in asynchronous method, your code is not asynchronous; full stop. Commented Aug 31, 2023 at 17:04
  • I think you are getting "delay" and "asynchronous" confused. A delay is always a synchronous thing, For example, run the foo function, then wait for 2 seconds, then do something else. That's synchronous. it's just that to get/implement the delay, you use asynchronous APIs. Commented Aug 31, 2023 at 17:06
  • @ScottMarcus "A delay is always a synchronous thing" - uh, no. You can build delays in your code both synchronously (with a blocking sleep, doing nothing until a timer is reached) and asynchronously (scheduling things for later when a timer expires) Commented Aug 31, 2023 at 17:09
  • @Bergi I'm talking about the functional result of having a delay. A delay in your code is something that happens before or after something else, not in parallel. Implementing a delay is done with asynchronous API's. I think the OP is confusing the two. Commented Aug 31, 2023 at 17:12
  • @Bergi You can build delays in your code both synchronously (with a blocking sleep, doing nothing until a timer is reached) <-- Yes, but to build such a synchronous blocker, you need asynchronous code (the timer). The delay itself happens as a synchronous event. Commented Aug 31, 2023 at 17:14

1 Answer 1

0

You can't.

You get asynchronous functionality when you use asynchronous features.

If you restrict yourself to synchronous features then you can only get synchronous results.

Sign up to request clarification or add additional context in comments.

4 Comments

Callbacks, Promises, Async/Await are asynchronous features. You mean without built-in asynchronous methods? Can you make the code non-blocking/asynchronous with Date() just using callbacks, promises, async/await and not using a built-in method like setTimeout()?
@kavron — I have no idea what you mean by, or even which part of this answer you refer to with, that sentence.
@kavron Not all callbacks are asynchronous. Callbacks are just one of the patterns used by asynchronous code.
@kavron "Can you make the code non-blocking/asynchronous with Date() just using callbacks, promises, async/await and not using a built-in method like setTimeout()?" - the very first sentence in this post answers that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.