danielgtaylor / nesh

An enhanced, extensible interactive shell for Node.js and CoffeeScript
http://danielgtaylor.github.io/nesh/
MIT License
288 stars 27 forks source link

Integrate promirepl #49

Open forivall opened 8 years ago

forivall commented 8 years ago

https://github.com/building5/node-promirepl :heart:

I'll probably write a PR for this, since it's just mostly copyingforking their code, and can be completely optional via the .promise togglability

hems commented 6 years ago

@danielgtaylor how could we add a function to receive the evaluated value before returning the evaluated value to the shell?

Ideally, we would just check if it's a promise and if it's a promise we would "await" on it, I'm sure it wouldn't be complicated, it's just about "spoofing" the eval method?

hems commented 6 years ago

That's how i worked it out:

    nesh.start options, ( error, repl ) ->

      # report error
      if error then console.log nesh.log.error error

      old_eval = repl.eval

      repl.eval = ( input, context, filename, callback ) ->

        old_eval input, context, filename, ( error, value ) ->

          if error then return callback error

          if value instanceof Promise

            try
              result = await value
              return callback null, result
            catch error
              return callback error

          callback error, value