1

The other day, I wrote this function in JavaScript as a simple DTO for an object from a jQuery plugin. I assumed that if I wrote a general return gridColumns line at the end of the function, the array could possibly be returned before my for loop was finished populating it. So I wrote this while (true) statement at the end, thinking I was being clever.

 function getGridColumns() {
 var gridColumns = [];
 var records = $("#jqxGrid").jqxGrid("columns").records;
 for (var i = 0; i < (records.length); i++) {
 var obj = {
 datafield: records[i].datafield,
 width: records[i].width,
 cellsalign: records[i].cellsalign,
 hidden: records[i].hidden
 }
 gridColumns.push(obj);
 }
 while (true) {
 if (gridColumns.length == records.length {
 return gridColumns;
 }
 }
 };

A buddy of mine looked at my little "hack" and said it was completely unnecessary, and I did some testing and determined that he's right.

So, here's where I'm at. How is JavaScript asynchronous, and how is it not? Can anyone help me understand this paradigm so that I can write better JavaScript?

asked Oct 12, 2015 at 13:56
4
  • 3
    It's asynchronous when you make a function call to an asynchronous service. Nothing in your code above fits that description. Commented Oct 12, 2015 at 13:58
  • @Pointy so async has to be explicitly defined, otherwise JavaScript will execute synchronously? Commented Oct 12, 2015 at 13:59
  • And thanks for the downvote, whoever did that. Really constructive for people looking to learn, like myself. Commented Oct 12, 2015 at 14:01
  • 1
    Yes, normal JavaScript code executes synchronously. Services provided by the platform (web browser or Node, for example) may be asynchronous, and when that's the case there will be some way to pass a callback that will be invoked when the service operation completes. It's never a good idea to introduce a "busy wait" loop as in your code above. Commented Oct 12, 2015 at 14:05

1 Answer 1

2

The most common mechanisms that spawn new "threads" of execution and thus so to speak introduce an asynchronous situation in javascript are AJAX call callbacks (unless the call was specifically made synchrounous) and setInterval() and setTimeout() calls. As pointed out by Pointy, there are more than this though.

answered Oct 12, 2015 at 14:02
Sign up to request clarification or add additional context in comments.

3 Comments

There are other services that introduce asynchronous bahaviors in browsers, and many more in environments like Node.js.
Thanks Pointy, had a feeling I didn't really know this well enough. Updated my answer to say "most common" instead of "all".
Certainly those are the most common, or at least the first things encountered by new JavaScript programmers :) Another big source of questions about asynchronous behavior are APIs like Google Maps, which of course hides AJAX operations inside it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.