Skip to content

Promise eval in repl yields inaccessible object #18905

Closed
@tomas

Description

@tomas
  • Version: v8.9.4
  • Platform: Linux x64
  • Subsystem: repl

I'm trying to build a custom repl that yields promises before returning the value to the user, but for some reason I'm unable to "reach" the final value, even though it does appear to be correctly assigned.

This is the code I'm using:

var repl = require('repl'),
    vm   = require('vm');

function foo() {
  return new Promise(function(resolve, reject) {
    resolve({ success: true })
  })
}

function myEval(code, context, file, cb) {
  var res = vm.runInContext(code, context)

  if (!res || typeof res.then != 'function') // Non-thenable response
    return cb(null, res)

  res.then(function(val) {
    cb(null, val)
  }, function(err) {
    cb(err)
  })
}

var replServer = repl.start({
  eval: myEval
})

replServer.context.foo = foo;

And this is what happens:

> res = foo()
{ success: true }
> res.success
undefined
> res.constructor
[Function: Promise]

However, the "real" result does get correctly assigned to _:

> res = foo()
{ success: true }
> _.success
true

Is there something I'm missing?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions