node.jspromiseqnode-repl

Getting data from fulfilled Q promise object in node REPL


Consider this simple Q promise object:

nesh> var p = functionThatReturnsPromise();

The REPL is kind enough to output the promise's state and value if I go:

nesh> p
{ state: 'fulfilled',
  value: 
   {
     // (data properties)
   }
}

Suppose I indeed waited for the promise to fulfill, I can't get the value nor the state directly by p.value or p.state.

I can do something like:

nesh> var data
undefined
nesh> p.then(function(_data) { data = _data })

yet it feels clumsy and uncomfortable for fluent REPL workflow.

Any ideas?


Solution

  • var p = functionThatReturnsPromise();
    

    Promises do have the state and value defined, but for accessing that you need to use the valueOf() function over this.

    p.valueOf() ==> promise value
    p.inspect() ==> { state: 'fulfilled', value: 'data' }