Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3761F200D68 for ; Thu, 28 Dec 2017 15:07:05 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 35EE5160C1F; Thu, 28 Dec 2017 14:07:05 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 546D9160C16 for ; Thu, 28 Dec 2017 15:07:04 +0100 (CET) Received: (qmail 45673 invoked by uid 500); 28 Dec 2017 14:07:03 -0000 Mailing-List: contact dev-help@thrift.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@thrift.apache.org Delivered-To: mailing list dev@thrift.apache.org Received: (qmail 45660 invoked by uid 99); 28 Dec 2017 14:07:03 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Dec 2017 14:07:03 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id DAA511A0D01 for ; Thu, 28 Dec 2017 14:07:02 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -99.201 X-Spam-Level: X-Spam-Status: No, score=-99.201 tagged_above=-999 required=6.31 tests=[KAM_ASCII_DIVIDERS=0.8, KAM_SHORT=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.001, SPF_PASS=-0.001, USER_IN_WHITELIST=-100] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id ubpE2R1vF7eX for ; Thu, 28 Dec 2017 14:07:01 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id DA9345F24E for ; Thu, 28 Dec 2017 14:07:00 +0000 (UTC) Received: from jira-lw-us.apache.org (unknown [207.244.88.139]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id 54D0BE012B for ; Thu, 28 Dec 2017 14:07:00 +0000 (UTC) Received: from jira-lw-us.apache.org (localhost [127.0.0.1]) by jira-lw-us.apache.org (ASF Mail Server at jira-lw-us.apache.org) with ESMTP id 0F4C5212F7 for ; Thu, 28 Dec 2017 14:07:00 +0000 (UTC) Date: Thu, 28 Dec 2017 14:07:00 +0000 (UTC) From: "ASF GitHub Bot (JIRA)" To: dev@thrift.apache.org Message-ID: In-Reply-To: References: Subject: [jira] [Commented] (THRIFT-4406) JavaScript: Use modern Promise implementations MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-JIRA-FingerPrint: 30527f35849b9dde25b450d4833f0394 archived-at: Thu, 28 Dec 2017 14:07:05 -0000 [ 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 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)