2

I wish to validate my assumptions:

As I read yet, to create an async call, I must eventually end up calling a javascript native call (like setTimeout() || XMLHttpRequest() || ...) to get out of the engine main scheduler loop.

Any third party library wishing to be async, will indirectly do the same.

Is this right, or is there a way to create my own async code (for instance by allowing a blocking feature on my code) intentionally without a js native call ?

Alain
  • 1,450
  • 3
  • 20
  • 37
  • 1
    WebWorker could offer a way. Depends, if you regard them as native calls. – Sirko Nov 12 '14 at 20:14
  • ...what exactly are you asking? The question in your post title is different than the ones you're asking in your question's description, if I understand correctly – elzi Nov 12 '14 at 20:14
  • 1
    to be clear: JS itself provides no way of doing async anything (afaik), you have to use something provided by the host environment, like setTimeout or ajax. – dandavis Nov 12 '14 at 20:25
  • @Alain is my answer not what you were looking for? – Don Rhummy Nov 12 '14 at 23:47

1 Answers1

2

In JavaScript, there are a few ways to write code that can be run asynchronously. Typically the methods used were:

  1. setTimeout
  2. setInterval
  3. XMLHttpRequest

But in HTML5 (and beyond) there are a lot of other methods that work asynchronously. For example, the new asynchronous file uploading, Web Workers and Promises. (NOTE: not all of these are supported by every browser)

Your question, though, is vague. What are you attempting to do? If it's UI-centric, you may want to look at requestAnimationFrame

If by blocking, you mean you want to show a "loading" gif and then go do stuff and change that after the "stuff" is done, there are a lot of ways to handle that but with the newer browsers, you'd use Promises: http://www.html5rocks.com/en/tutorials/es6/promises/ with a check for some value in a timeout function.

//The value to check
var isDone = false;

var asynchCheck = function(resolve, reject) {

    if ( notStarted )
    {
         // do stuff (calls async function that eventually sets "isDone" = true)
    }

    if ( isDone === true )
    {
        if ( no errors ) resolve("Stuff worked!");
        else reject(Error("It broke"));
    }

    //Check again
    else setTimeout( asynchCheck, 50 );
}
//Start your function
var promise = new Promise(asynchCheck);

//Now set the success/fail functions
promise.then(function(result) {
  console.log(result); // "Stuff worked!"
}, function(err) {
  console.log(err); // Error: "It broke"
});
Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • 2
    promises alone can't create async, you still need an async interface to wrap. – dandavis Nov 12 '14 at 20:26
  • @dandavis http://stackoverflow.com/questions/12683510/ios-6-safari-setinterval-doesnt-get-fired I can't find it now but there's also an issue on mobile browsers if your `setInterval` delay is too short. – Don Rhummy Nov 12 '14 at 20:36
  • @dandavis I removed it but I ran into issues recently on iOS with setInterval, just can't remember what they were – Don Rhummy Nov 12 '14 at 20:46
  • @Don, my question is vague cause it apply to javascript in general. However, I am actually studying the promises/a+, and I am trying to do async without setTimeout or any native call. I cannot seem to find any custom made async call I could construct on my own, except by eventually using a js native call, eventually. If no more answer, I will accept your answer. – Alain Nov 13 '14 at 01:33
  • 1
    @Alain read about threading in JavaScript and you'll see the reason you can't do async without those native calls is because JavaScript is single threaded. – Don Rhummy Nov 13 '14 at 04:43