New Promise Syntax Proposal

Jorge Téllez novohispano at gmail.com
Mon Nov 6 15:39:47 UTC 2017


Yes, I’ll be happy to provide a concrete example. 

Let’s say I am creating a CLI using readline.

```js
const readline  = require('readline');
const interface = readline.createInterface({
  input:  process.stdin,
  output: process.stdout
});

async function run() {
  var name = '';
  var base = 0;

  console.log('** Bienvenido al Calculador de Área **');

  name = await requestInput(‘What’s your name?\n');
  console.log(`Hello, ${name}!`);
}

function requestInput(question) {
  return new Promise(function(resolve) {
    interface.question(question, function(input) {
      resolve(input);
    })
  })
}

run();
```

Instead of defining a wrapper function that returns a promise, I would like to define the promise at the same time like this:

```js
const readline  = require('readline');
const interface = readline.createInterface({
  input:  process.stdin,
  output: process.stdout
});

async function run() {
  console.log('** Bienvenido al Calculador de Área **');

  const name = await requestInput(‘What’s your name?\n');
  console.log(`Hello, ${name}!`);
}

promise requestInput(resolve, reject, question) {
  interface.question(question, function(input) {
    resolve(input);
  })
}

run();
```
__
Jorge Téllez
+52 1 81 2567 8257
@novohispano <http://twitter.com/novohispano>

> On Nov 6, 2017, at 9:33 AM, Jonathan Barronville <jonathan at belairlabs.com> wrote:
> 
> From the example provided, as someone who uses promises a lot, I’m not sure I’m sold on the need for this either. Maybe you could provide some more concrete examples, Jorge?
> 
> 
> P.S. Proposals like this are why JavaScript should’ve been a LISP ;p …
> 
> On Mon, Nov 6, 2017 at 10:28 AM, Isiah Meadows <isiahmeadows at gmail.com <mailto:isiahmeadows at gmail.com>> wrote:
> I'm not convinced of the need. Promises are already sufficient, and in general use, I rarely use the constructor outside of adapting callback-related code or other lower-level cases.
> 
> Also, keep in mind, most such promise-returning functions do have arguments, which this proposal seems to miss.
> 
> 
> On Mon, Nov 6, 2017, 10:23 Jorge Téllez <novohispano at gmail.com <mailto:novohispano at gmail.com>> wrote:
> I would like to propose a new syntax for promises for the next ECMAScript.
> 
> It is common to define promises in the following way:
> 
> function promiseFunction() {
>   return new Promise(resolve, reject) {
>     resolve(someValue);
>   };
> }
> 
> In the previous example, I am declaring a function so that I can access the promise throughout. 
> 
> I would like propose a simpler syntax to remove this redundancy:
> 
> promise promiseFunction(resolve, reject) {
>   resolve(someValue);
> }
> 
> This will make the promise declaration easier to read in a similar fashion as the new class syntax made it easier to declare prototypes.
> 
> __
> Jorge Téllez
> +52 1 81 2567 8257 <tel:+52%201%2081%202567%208257>
> @novohispano <http://twitter.com/novohispano>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> 
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org <mailto:es-discuss at mozilla.org>
> https://mail.mozilla.org/listinfo/es-discuss <https://mail.mozilla.org/listinfo/es-discuss>
> 
> 
> 
> 
> -- 
> - Jonathan
> 
>> 
> Life is a game and we’re all just high density pixels.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20171106/90ed9841/attachment-0001.html>


More information about the es-discuss mailing list