2

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?

Kludge
  • 2,653
  • 4
  • 20
  • 42
  • It's not "clumsy" or "uncomfortable", that's the intended behaviour for promises. `p` only holds the reference to the promise, if you want to check the value then you use `.then` and can inspect the value there. – Ben Fortune Jul 03 '15 at 14:23
  • I know what the intended behavior is. Again, I'd like to have a more fluent workflow within the REPL. If I type the promise object's name and it outputs a magical object { state: ... , value: ... }, then asking access to it is not too much to ask. – Kludge Jul 03 '15 at 14:39

2 Answers2

3
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' }
nitishagar
  • 9,038
  • 3
  • 28
  • 40
0

You can try this:

p.then(function (value) { debugger; });

Then "continue" -- execution will stop when promise is fulfilled and callback is called. Note however, it seems to freeze, node 0.12.4, though I'd think this was a node bug -- perhaps will work for you.

shaunc
  • 5,317
  • 4
  • 43
  • 58