简体   繁体   中英

How Async really works and How to use it properly with node.js (node-webkit)

efor this problem i am using Node-Webkit (node.js) and Async , loading a Windows App.

The reason of this question is to definitively answer:

What really means asynchronous execution in Javascript and Node.Js.

My personal code problem is at the end of the Question. "The Case".

I am going to explain all about the problem i have directly with a schematic summary. (And I will be updating the info as you help me to understand it)


The Concept (theory)

Imagine a Primary Screen (JS, Html, css, ... Node.js frameworks) and a Background Procedure (JS execution every 10 min, JS internal checks, background Database Optimization, ...).

Whatever you do in Primary Screen wont affect background execution (except some important cases), and Background can change even the Screen if he needs to (screen timers, info about online web status, ...)

Then the behaviour is like:

Thread 1: Your actions inside the App framework. Thread 2: Background App routines

Any action as they finish gives his output to screen, despite of the rest of the actions in async parallel


The Interpretation (For me)

I think this is something that "Async" will handle without problems, as a parallel execution.

async.parallel([
function(){ ... },
function(){ ... }
], callback);      //optional callback

So the Thread 1 and Thread 2 can work together correctly while they do not affect the same code or instruction. The Content will be changing while any threads request something of/to it.


The Implementation (Reality) Code is not fully asynchronous during the execution, there are sync parts with common actions, that when they need calls the async codes.

Sync: Startup with containers -> Async: load multiple content and do general stuff -> Sync: Do an action in the screen -> ...


The Case

So here it is my not working properly code:

win.on('loaded', function() {

 $( "#ContentProgram" ).load( "view/launcherWorkSpace.html", function() {
$("#bgLauncher").hide();
win.show();

async.parallel([
    function()   //**Background Process: Access to DB and return HTML content**
    {       
            var datacontent = new data.GetActiveData();  
        var exeSQL = new data.conn(datacontent);
            if(exeSQL.Res) 
            {
                var r = exeSQL.Content;
                if(r.Found)
                {
                    logSalon = new data.activeSData(r)
                    $('#RelativeInfo').empty();
                    $("#RelativeInfo").html("<h4 class='text-success'>Data found: <b>" + logData.getName + "</b></h4>");
                }
            }
    },
    function()  //**Foreground Process: See an effect on screen during load.**
    {
        $("#bgLauncher").fadeIn(400);
        $("#centralAccess").delay(500).animate({bottom:0},200);
    }
]);
});
});

As you can see, im not using "Callback()" because i dont need to (and it does the same).

I want to do the Foreground Process even if Background Process is not finished, but the result of the code is done at same time when both request has finished...

If i disconect the DB manually, first function takes 3 seconds until gives an exception (that i wont handle). Until then, both proccess will not output (show on screen) anything. (Foreground Process should be launched whatever happends to Background Process).

Thanks and sorry for so much explanation for something that looks like trivial.


EDITED

This start to be annoying... I tried without Async, just a javascript with callback like this:

            launchEffect(function () {
            var datacontent = new data.GetActiveData();

            var exeSQL = new data.conn(datacontent);

            if(exeSQL.Res) 
            {
                var r = exeSQL.Content;

                if(r.Found)
                {
                    logData = new data.activeData(r)
                    $('#RelativeInfo').empty();
                    $("#RelativeInfo").html("<h4 class='text-success'>Salón: <b>" + log.getName + "</b></h4>");
                }
            }
        });
    });

});
function launchEffect(callback)
{
  $("#bgLauncher").fadeIn(400);
    $("#centralAccess").delay(500).animate({bottom:0},200);
    callback();
}

Even with this... Jquery doesnt work until the callback answer...

node-webkit let's you run code written like code for node.js, but is ultimately just a shim running in WebKit's Javascript runtime and only has one thread, which means that most 'asynchronous' code will still block the execution of any other code.

If you were running node.js itself, you'd see different behavior because it can do genuinely asynchronous threading behind the scenes. If you want more threads, you'll need to supply them in your host app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM