Asynchronous output

Javascript and Node.JS make heavy use of asynchronous execution. IJavascript lets you exercise these asynchronous capabilities, both:

  • by updating stdout and stderr asynchronously, or
  • by updating the IJavascript output asynchronously.

Updating stdout and stderr asynchronously

Both streams stdout and stderr can be written asynchronously. Any text written to these streams will be forwarded back to the latest request from the frontend:


In [1]:
function countTo(number, milliseconds) {
    var counter = function() {
        console.log(counter._n++);
        
        if (counter._n > number) {
            clearInterval(counter._intervalObject);
            console.warn("Done!");
        }
    };
    counter._n = 1;
    counter._intervalObject = setInterval(counter, milliseconds);
}

countTo(5, 1000);


Out[1]:
undefined
1
2
3
4
5
Done!

Updating the IJavascript output asynchronously

IJavascript offers two global definitions to help produce an output asynchronously: $$async$$ and $$done$$().

When $$async$$ is set to true, the IJavascript kernel is instructed not to produce an output. Instead, an output can be produced by calling $$done$$().


In [2]:
$$async$$ = true;

console.log("Hello!");

setTimeout(
    function() {
        $$done$$("And good bye!");
    },
    1000
);


Hello!
Out[2]:
'And good bye!'

It is also possible to produce a graphical output asynchronously, the same way it is done synchronously, with the difference that $$done$$() has to be called to instruct the IJavascript kernel that the output is ready:


In [3]:
$$async$$ = true;

console.log("Hello!");

setTimeout(
    function() {
        $$svg$$ = "<svg><circle cx='30px' cy='30px' r='20px'/></svg>";
        $$done$$();
    },
    1000
);


Hello!
Out[3]: