asvd / jailed

execute untrusted code with custom permissions
MIT License
1.01k stars 69 forks source link

Support promises on top of callbacks #18

Closed andrey-skl closed 8 years ago

andrey-skl commented 8 years ago

Communication with child process is built on top of postMessage now, and that's why it doesn't support returning value. And we have to use that old-style callbacks to return value from function.

It would be much better to wrap that functions into promises to allow returning values as well as promises and all such things.

Example of current plugin code:

application.setInterface({
  getSomeValue: function (callback) {
     callback(123);
  },
  doAsyncJob: function (callback) {
     setTimeout(callback, 1000);
  }
});

What it could look like:

application.setInterface({
  getSomeValue: function (callback) {
     return 123; //return simple value from promise is ok
  },
  doAsyncJob: function () {
    return new Promise(function(resolve) {
     setTimeout(resolve, 1000);
    });
  }
});

And then you can call plugin code from your App so easy:

plugin.remote.doAsyncJob()
   .then(doSomethingElse);
asvd commented 8 years ago

Hello Andrey, thanks for your suggestion. This will not be implemented, because Promises is just a syntacsic sugar over the callbacks - you still provide a callback to the Promise constructor which as result makes the code more complex without a reason.

It is technically possible to handle the returned value of an exported method and send the result over a message, but since messages are handled asynchronously, it is only possible to handle the result in some sort of callback on the oppsite side (no matter if this is an ordinary callback, or sugared with a Promise pattern). Therefore in order to keep the code simplier and consistent, returned value is ignored, and callbacks are suggested to be used instead. This make the exported function behave in the way as if it would be used directly.

Also I don't like Promises ;-)

andrey-skl commented 8 years ago

@asvd Okay and thank you for the honest reply.