-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.

kavron
  • 1
  • 1
    If you're not invoking a built-in asynchronous method, your code is not asynchronous; full stop. – Bergi Aug 31 '23 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. – Scott Marcus Aug 31 '23 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) – Bergi Aug 31 '23 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. – Scott Marcus Aug 31 '23 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. – Scott Marcus Aug 31 '23 at 17:14
  • @ScottMarcus I wouldn't say that the system clock (or `Date.now()`) is an asynchronous mechanism. And you don't even need a timer when you know the clock speed of the CPU (common in microcontrollers), you can just loop with a counter. – Bergi Aug 31 '23 at 17:25
  • @ScottMarcus Not exactly I'm confused with delay. But it's easy to prove that something is asynchronously working if the code isn't blocking and due delays a loop finishes disordered. – kavron Aug 31 '23 at 18:10

1 Answers1

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 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 Aug 31 '23 at 19:42
  • 1
    @kavron — I have no idea what you mean by, or even which part of this answer you refer to with, that sentence. – Quentin Aug 31 '23 at 19:44
  • 1
    @kavron [Not all callbacks are asynchronous](https://stackoverflow.com/q/21884258/1048572). Callbacks are just one of the patterns used by asynchronous code. – Bergi Aug 31 '23 at 19:49
  • 1
    @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. – Bergi Aug 31 '23 at 19:50