Search code examples
oopprogramming-languagesasynchronous

needs for synchronous programming


EDIT: This question was misexpressed. What I've really wanted to ask was:

Is there anything what cant be written in OO languages (with support for closures) using continuation-passing style?

You can google what CPS does mean or just stick with definition of function/method never returning anything, always pushing data somewhere - using passed callback.

And after yers from original question, I can even answer myself - there's nothing like that. And moreover it's actually very good OO principle called Tell Dont Ask

function getName(){
  return this.name;
}

console.log(xyz.getName())

vs.

function pushNameTo(callback){
  callback(this.name);
}

xyz.pushNameTo(console.log)

good, but this time it was named after how it does the thing, lets name it after what it does and make it even more OO:

function renderOn(responseBuilder){
  var b = responseBuilder;

  //or just string, whatever, depending on your builder implementation
  b.field("Name: ", this.name);
  b.field("Age: ", this.age);
  b.image("Profile photo", this.imageData);
}

person.renderOn(htmlBuilder);

the point here is - the object encapsulates not only its data but even behavior, the spirit, personality. Who else should be responsible for expressing person's representation rather than person itself?

Of course this does not necessarily means you should have html in your code, builder serves this purpose. It can even generate some xml or other data-format for actual UI-rendering layer. But its always push instead of pull.


Solution

  • Nothing, of course. Consider: if you have a program that is completely sequential, you could simply insert it into some kind of wrapper, like document.onload(). Then the sequential program would be started asynchronously.

    Going the other way around, if all you have is a synchronous language, you can always write the asynchronous case by having a table of pieces to be executed, and an inner loop that looks to see what's been enabled, and takes it from the table to execute. in fact, this would look very much like the underlying runtime in whoich your javascript runs.