Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a simple example of an asynchronous javascript function? [closed]

I am really struggling here to get to grips with writing asynchronous JavaScript. Could you please provide an example of a simple JavaScript function which is asynchronous written in plain JavaScript (and not using Node.js or JQuery)

like image 579
Jasdeep Khalsa Avatar asked Dec 10 '12 18:12

Jasdeep Khalsa


People also ask

What is an asynchronous function in JavaScript?

An asynchronous function is any function that delivers its result asynchronously – for example, a callback-based function or a Promise-based function. An async function is defined via special syntax, involving the keywords async and await . It is also called async/await due to these two keywords.

Which things are asynchronous in JavaScript?

Asynchronous programming is a technique that enables your program to start a potentially long-running task and still be able to be responsive to other events while that task runs, rather than having to wait until that task has finished. Once that task has finished, your program is presented with the result.

Is closure is asynchronous operation in JavaScript?

Using ClosuresJavaScript can be asynchronous when it comes to execution. When an operation is completed, a callback has to be employed. This callback now has to run with the same execution context as its caller/parent.

What is asynchronous programming example?

Here's an example: Data may take long a long time to submit to a database. With asynchronous programming, the user can move to another screen while the function continues to execute. When a photo is loaded and sent on Instagram, the user does not have to stay on the same screen waiting for the photo to finish loading.


2 Answers

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):

window.setTimeout(function() {
    console.log("World");
}, 1000);
console.log("Hello");

Update: Since ES6 there are promises as an asynchronous primitive built into plain JavaScript, so you can do

 Promise.resolve("World").then(console.log); // then callbacks are always asynchronous
 console.log("Hello");

However, on their own they're not really helpful when there is nothing you could wait for (such as a timeout). And they don't change anything about the threading model either, all execution is run-to-completion without any events interfering midway.

like image 115
Bergi Avatar answered Oct 08 '22 16:10

Bergi


This is asynchronous:

setTimeout(function(){
   console.log('1');
}, 2000);

console.log('2');

2 will be written to the console before 1. Because setTimeout is async.

like image 31
Peter Rasmussen Avatar answered Oct 08 '22 14:10

Peter Rasmussen