Closed Redsandro closed 11 years ago
This is happening to all promises, not just the once created by q.nfcall
. I first encountered this trying to get an array from promise.allResolved()
. I get a promise, even in the valueOf
that promise. I can traverse the array inside the promise chain (then()
), but then the array items are promises for which I cannot reach their values.
Once a starship is returned as a promise, there is no escape from the chain. We are trying to reach our home world, which lies outside the chain. Please show us how to correctly change our heading. We are desperate for food and supplies.
I now realize that a promise that not is fulfilled yet cannot reveal it's value, but I am wondering if the valueOf()
shouldn't be undefined
in this case.
@redsandro Tough call. undefined
is a perfectly meaningful fulfillment value, but a promise is not.
Hmm.. this is true. Back when things took time in my node script, I was getting used to being able to use valueOf()
so that I forgot to check that this is actually documented in the API wiki.
I guess in stead of checking for undefined
I can do something like if (myPromise.valueOf().constructor && myPromise.valueOf().constructor.name == 'Promise')
for now, and start converting all such occurances in my code to a callback-stype wrapper functions to pass along promised values.
Use !promise.isPending() as your guard. Generally though, use "then" unless you really need to optimize.
On Tuesday, March 19, 2013, Redsandro wrote:
Hmm.. this is true. Back when things took time in my node script, I was getting used to being able to use valueOf() that I forgot to check that this is actually documented in the API wiki.
I guess in stead of checking for undefined I can do something like if (myPromise.valueOf().constructor && myPromise.valueOf().constructor.name== 'Promise')for now, and start converting all such occurances in my code to a callback-stype wrapper functions to pass along promised values.
— Reply to this email directly or view it on GitHubhttps://github.com/kriskowal/q/issues/237#issuecomment-15151588 .
Thank you.
To be fair, there's some agreement that the existing synchronous-inspection mechanisms are confusing, and might be replaced or augmented with something like https://github.com/promises-aplus/synchronous-inspection-spec/issues/6
I agree that this could be improved upon, but in my own prosecution, one might argue that in an ideal promised land, values won't ever have to be evaluated like this.
Either way, I like that this is being debated. :)
One thing though about events in stead of promises is that breakpoint debugging is a great way to examine long storylines of code. But the value of a promise in an entire chain cannot be followed in a similar fashion this way, because the value won't be known until a few ticks later.
Any hints on how to do something similar in promiseland?
I cannot get the value out of a promise.
valueOf
seems to be the way to do this, but it is either broken or I misunderstood how to use it.Testcase
q.valueof.js
Terminal output