Asynchronous JavaScript function

What is a simple example of an asynchronous JavaScript function?

Last updated:7/19/2021 5:24:50 AM

1 Answers

Ethan Karla
Ethan Karla

JavaScript itself is synchronous and single-threaded. You cannot write an asynchronous function; plain JS has no timing API. There will be no side-effects from parallel threads.
What you can do is use some APIs provided by your environment (Node.js, Webbrowser) that allow you to schedule asynchronous tasks - using timeouts, ajax, FileAPI, requestAnimationFrame, nextTick, WebWorkers, DOM events, whatever.
An example using setTimeout (provided by the HTML Timing API):
<!DOCTYPE html>
<html lang='en' xmlns=''>
    <meta charset='utf-8' />
    <title>Asynchronous JavaScript </title>
        window.setTimeout(function () {
            console.log('Hello World');
        }, 1000);
<body align='center'>
    <h1>Asynchronous JavaScript</h1>

Hope this information has cleared your confusion.

Happy Coding!