Promise.valueOf() returns a promise, not the value of the promise.
See original GitHub issueI 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
#!/usr/bin/env node
var util = require('util'),
q = require('q');
util.debug('Test promise.valueOf()...');
var someFunction = function(val, cb){cb(null, val);};
var someValue = 'shiboleet';
var myPromise = q.nfcall(someFunction, someValue).then(function(val){
util.debug('Expect: "shiboleet" (1)');
util.log(val); // String: shiboleet
return val;
});
util.debug('Expect: "shiboleet" (2)');
util.log(myPromise.valueOf()); // Object: Promise
Terminal output
$ ./q.valueof.js
DEBUG: Test promise.valueOf()...
DEBUG: Expect: "shiboleet" (2)
19 Mar 18:34:43 - [object Promise]
DEBUG: Expect: "shiboleet" (1)
19 Mar 18:34:43 - shiboleet
Issue Analytics
- State:
- Created 11 years ago
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Promise.resolve() - JavaScript - MDN Web Docs
The Promise.resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the...
Read more >Why does this resolved Q promise responds to valueOf() with ...
The valueOf method is useful for providing information about the promise in the same turn of the event loop. For example, resolved promises...
Read more >Javascript: How to access the return value of a Promise object
It's really important to note that the Promise object doesn't return a value, it resolves a value via the then() method. It is...
Read more >Promise - JavaScript - UDN Web Docs: MDN Backup
A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with...
Read more >How to access the Value of a Promise in JavaScript | bobbyhadz
If you don't return a value from the callback function, you implicitly return undefined , which gets wrapped in a promise and returned...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@redsandro Tough call.
undefined
is a perfectly meaningful fulfillment value, but a promise is not.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 beundefined
in this case.