Search code examples
asynchronouswshjscript

Is asynchronous call of JavaScript function possible in Windows Script Host?


Imagine you have a simple function in Windows Script Host (JScript) environment:

function say(text) {
    WScript.Sleep(5000);
    WScript.Echo(text);
}

Is it possible to call say() asynchronously?

Note: Such browser-based methods as setInterval() or setTimeout are not available in WSH.


Solution

  • No, Windows Script Host doesn't support calling script functions asynchronously. You'll have to run two scripts simultaneously to achieve this effect:

    // [main.js]
    var oShell = new ActiveXObject("WScript.Shell");
    oShell.Run(WScript.FullName + " say.js Hello");
    
    WScript.Echo("Hello from main");
    

     

    // [say.js]
    WScript.Sleep(5000);
    WScript.Echo(WScript.Arguments.Item(0));