jashkenas / coffeescript

Unfancy JavaScript
https://coffeescript.org/
MIT License
16.46k stars 1.98k forks source link

[CS2] Enhancement: Support for 'await' on REPL without having to wrap it inside a function #4603

Closed AlvaroBernalG closed 7 years ago

AlvaroBernalG commented 7 years ago

Hello good people of coffeescript:

Currently, the way I can test how a library that heavily rely on promises works on the REPL is as follow:

coffee> wikijs = require('wikijs')
coffee> batman = ""
coffee> wikijs().page('batman'). then (response)=> batman = response
Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }
coffee>  batman
' '  // too soon.. the promise hasn't resolved yet
coffee>  batman 
{
  links: [Function: links],
  categories: [Function: categories],
  coordinates: [Function: coordinates],
  info: [Function: n],
  backlinks: [Function: backlinks],
  rawImages: [Function: f],
  mainImage: [Function: mainImage]
 }
coffee>  mainImage = ''
coffee> wikijs().page('batman'). then (response)=> response.mainImage()). then ( img) => mainImage = img
Promise {
  <pending>,
  domain: 
   Domain {
     domain: null,
     _events: { error: [Function: debugDomainError] },
     _eventsCount: 1,
     _maxListeners: undefined,
     members: [] } }
coffee> mainImage
'https://upload.wikimedia.org/wikipedia/en/7/74/Batman_Detective_Comics_Vol_2_1.png'

I can also define a function, but doing that on a REPL is awkward. I would rather create a file and set up an environment that auto run my script on save instead.

It would great if I could just take advantage of the new async/await features of coffeescript 2.0 and do this instead:

coffee> wikijs = require('wikijs')
coffee> batman = await wikijs().page('batman')
coffee> batman
{
  links: [Function: links],
  categories: [Function: categories],
  coordinates: [Function: coordinates],
  info: [Function: n],
  backlinks: [Function: backlinks],
  rawImages: [Function: f],
  mainImage: [Function: mainImage]
 }
coffee> await batman.mainImage()
'https://upload.wikimedia.org/wikipedia/en/7/74/Batman_Detective_Comics_Vol_2_1'

Which adjust perfectly with the philosophy of a REPL which is providing you with an environment to quickly and easily test and prototype stuff out.

lydell commented 7 years ago

@AlvaroBernalG Could you add an example showing how the Node.js REPL handles this? (Include your Node.js version)

AlvaroBernalG commented 7 years ago

v8.1.2 Same behaviour.

lydell commented 7 years ago

The CoffeeScript REPL uses the Node.js REPL. So if the Node.js REPL can't handle this, CoffeeScript can't either.

AlvaroBernalG commented 7 years ago

Wouldn't be possible to wrap the user input into 'async function main(){}' and then send it to the Node.js REPL?

So if you have something like this:

coffeescript> sleep = (ms)=> new Promise((resolve, reject) => setTimeout(resolve,ms))
coffeescript> await sleep(2000)

It would get transpiled into :

async function main(){
   var sleep;
   sleep = (ms)=> new Promise((resolve, reject) => setTimeout(resolve,ms))  
   console.log(await sleep(2000))
}
GeoffreyBooth commented 7 years ago
do -> await sleep(2000)
AlvaroBernalG commented 7 years ago

@GeoffreyBooth @lydell Thank you for your time :) .

P.S: Coffeescript 2 is looking great so far. I will definitely be using it in one of my next projects.

Thanks!