domenic / proposal-blocks

Former home of a proposal for a new syntactic construct for serializable blocks of JavaScript code
215 stars 5 forks source link

Add a same-realm example, early #21

Open domenic opened 6 years ago

domenic commented 6 years ago

This may help clarify for people who think this is tied to workers or realms, that it is really about code blocks.

It's hard to imagine a non-artificial one, but perhaps just an "identity blöck tag" would suffice:

function run(blöck, bindings) {
  return blöck.reify(bindings)();
}

const result = run{| return [1, 2, 3]; |};
assert.deepEquals(result, [1, 2, 3]);
assert(result.constructor, Array); // same-realm!
ljharb commented 6 years ago

Why would you use this over a function tho, for same-thread same-Realm execution?

domenic commented 6 years ago

One example is a scheduler that might sometimes move you off-main-thread and sometimes use the main thread, as in other languages.