Skip to content Skip to sidebar Skip to footer

How Can I Emulate A Synchronous Call With Generators In Javascript?

Suppose the next piece of code: var co = require('co'); var myObj = { getFieldValue: function() { var self = this; console.log(JSON.stringify(self)); i

Solution 1:

If it helps, I recently took the first release of the rogue written for UNIX in C and rewrote it for javascript to work in a browser. I used a technic called continuation to be able to wait for key entry by the user because in javascript the are no interrupts.

So I would have a piece of C code like this:

void function f() {

  // ... first part

  ch = getchar();

  // ... second part

}

that would be transformed in

functionf(f_higher_cont) {

  // ... first partvar ch = getchar(f_cont1);

  return;
  // the execution stops here functionf_cont1 () {

    // ... second part
    f_higher_cont();
  }
}

the continuation is then stored to be reuse on a keypressed event. With closures everything would be restarted where it stoped.

Post a Comment for "How Can I Emulate A Synchronous Call With Generators In Javascript?"