Javascript coverage asynchronously in synchronous

I am new to web development. Coming from Delphi / pascal. My long-term goal is to transform an outdated application into a web environment. (I am using this application: http://smartmobilestudio.com/ to cross compile pascal into javascript, but this should not affect this question). My legacy program uses synchronous remote procedure calls (RPC) for the server. I use EWD.js technology ( http://robtweed.wordpress.com/2014/01/23/ewd-js-on-fhir/comment-page-1/ ), which makes asynchronous calls through node.js. I read a lot of posts here about synchronous synchronous calls. And I’m afraid that I’m just out of luck if I can’t get EWD to act synchronously. But I want to understand this better.

Consider this pseudo code:

RPCcall
//business logic
RPCCall
//business logic
RPCCall
//business logic.

If any of the RPC calls fail, the entire application should fail. I read about “continuation style” coding, which I suppose is that every asynchronous call tells where to get the patch after completion, when the onMessage handler calls the callback function after receiving the return message:

MyProc()
  RPCCall(<name>,MyProc_2);
end;

MyProc2()
  //business logic
  RPCCall(<name, MyProc_3);
end;

MyProc3()
  //business logic
  RPCCall(<name, MyProc_3);
end.

And that would be possible, albeit awkward / ugly. But what about such situations?

RPCcall
//business logic
if conditionA then begin
  if conditionA2 then begin
    RPCCall
    //business logic
  end else
    RPCCall
    //business logic
  end else begin
    for i=1 to 10 do begin
      RPCCall
      //business logic
    end;
  end
end

I do not see realistically how to transform the above continuation style. If there is a call in the middle of a logical tree or loop, how can I return to this state? How was this done before? Incorrectly transcode an outdated application. It is very large and complex.

Any help would be appreciated. Thanks

+4
3

, AJAX jQuery. , , .

:

if(/*condition*/) {

function RPCCall() {
    return $.ajax({
        type: "GET",
        url: remote_url,
        async: false
    }).responseText;
}
   //business logic
}

AJAX nodejs, - :

var sequence = Futures.sequence();

sequence
  .then(function(next) {
     http.get({}, next);
  })
  .then(function(next, res) {
     res.on("data", next);
  })
  .then(function(next, d) {
     http.get({}, next);
  })
  .then(function(next, res) {
    ...
  })

, - .

  .then(function(next, d) {
    http.get({}, function(res) {
      next(res, d);
    });
  })
  .then(function(next, res, d) { })
    ...
  })

, .

+3

, .

- / . , jQuery, , nodejs ( nodejs jQuery?).

:

  • , - , - , - .
  • : , , , ,
  • , - , , , .

,

  • "()",
  • "done", , , .
  • "fail", , ,
  • "", , .
  • "reject", , .
  • , "", "done" -callbacks
  • , "reject", "fail" -callbacks

:

var conditionA = new Deferred();
RPCcall("rpccall1", function (result) {
    //buisness logic that determines state of conditionA
    if(something) {
         conditionA.resolve();
    } else {
         conditionA.reject();
    }
});
conditionA.done( function () {
    // RPCcall for conditionB and following business logic
});
conditionA.fail( function () {
   var conditions = [];
   for( var i = 0; i < 10; i++) {
       conditions[i] = new Deferred();
       // corresponding RPCcall and callbacks setup
       // i hope you get the idea
   }
});

, .

+2

, :

-, ewdRESTServer, ewdRESTS EWDServer, EWDServer ? rpc EWDServer ?

lib async.js,

take a look: How to handle errors in Node.js using Express

+1
source

Source: https://habr.com/ru/post/1535360/


All Articles