You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@thrift.apache.org by "ASF GitHub Bot (JIRA)" <ji...@apache.org> on 2017/12/28 14:07:00 UTC

[jira] [Commented] (THRIFT-4406) JavaScript: Use modern Promise implementations

    [ https://issues.apache.org/jira/browse/THRIFT-4406?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16305469#comment-16305469 ] 

ASF GitHub Bot commented on THRIFT-4406:
----------------------------------------

GitHub user bananer opened a pull request:

    https://github.com/apache/thrift/pull/1452

    THRIFT-4406: ES6 compatible JavaScript with Promises

    

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/bananer/thrift THRIFT-4406-pr

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/thrift/pull/1452.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #1452
    
----
commit ed1efba5ddd4d632bf53b0a9ef0b485b82979a04
Author: Philip Frank <ic...@...>
Date:   2017-12-06T11:38:05Z

    THRIFT-4406: Optionally generate ES6 compatible JavaScript where service methods return Promises
    
    includes tests and TypeScript support

----


> JavaScript: Use modern Promise implementations
> ----------------------------------------------
>
>                 Key: THRIFT-4406
>                 URL: https://issues.apache.org/jira/browse/THRIFT-4406
>             Project: Thrift
>          Issue Type: Improvement
>          Components: JavaScript - Compiler
>            Reporter: Philip Frank
>            Priority: Minor
>
> I would like to acces the results of service method invocations in a web browser environment using [Promises|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this] (or a compatible API). I'm willing to work on the necessary changes in the compiler and am seeking guidance regarding the best way to make these changes.
> Current state:
> In browser JS, i can pass a callback function as last argument when calling a method:
> {code:javascript}
>     service.getFoo('a1', 'a2', function(foo) {
>         alert(foo);
>     });
> {code}
> There is also a synchronous API, however this blocking way is discouraged by browser vendors. It also fails with an error for me when using WebSocketTransport instead of XHRTransport.
> {code:javascript}
>     alert(service.getFoo('a1', 'a2'));
> {code}
> In JS code generated for NodeJS, the 'q' library is included, which provides an API mostly compatible with JavaScript's native Promises.
> {code:javascript}
>     service.getFoo('a1', 'a2').then(function(foo) {
>         alert(foo);
>     });
> {code}
> From what I can tell 'q' is mostly irrelevant today since native Promises are supported in NodeJs [for many versions|http://node.green/#ES2015-built-ins-Promise].
> Regarding support in browsers, native Promises work in [recent versions of most browsers|https://caniuse.com/#feat=promises] and can be enabled in other cases by including one of the many polyfill libraries, like the popular [core-js|https://github.com/zloirock/core-js#ecmascript-6-promise].
> Proposed solution:
> In both browser and nodeJS, service methods return a Promise when invoked without a callback. This has many advantages for developers:
> * Promise API is an established standard in modern JavaScript (ECMAScript), it is easy for developers to consume and to use with other libraries
> * Error handling is possible through {{catch()}}
> * Result handling is very flexible through chaining
> * Promises can be combined through {{Promise.all()}} and {{Promise.race()}}
> * The [{{await}}|https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await] keyword makes it even easier to write async code. (Support limited to some browsers or with cross-compilers like [babel|https://babeljs.io/])
> However, this would break compatibility:
> * Synchronous (blocking) invocations would no longer be supported, instead a Promise instead of the value is returned
> * with NodeJS code relying on some features specific to 'q' (e.g. {{finally()}} and {{done()`)
> * Internet Explorer needs a polyfill for Promises
> I'm assuming the code generated by Thrift should stay backwards compatible in the foreseeable future, so I suggest making these changes behind a new compiler flag, {{-gen js:es6}} (for ECMAScript 6). This would also open up the ability to leverage more ES6 features in the future: For example, using {{const}} instead of {{var}} might give a slight performance improvement and using the {{class}} syntax could improve readability of the generated code.
> Please share any thoughts regarding this topic or hints towards a good implementation. If there are no objections, I'm willing to start working on this soon, with the hopes of getting my changes accepted into the project codebase.



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)