question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promise.valueOf() returns a promise, not the value of the promise.

See original GitHub issue

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

#!/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:closed
  • Created 11 years ago
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kriskowalcommented, Mar 19, 2013

@redsandro Tough call. undefined is a perfectly meaningful fulfillment value, but a promise is not.

1reaction
Redsandrocommented, Mar 19, 2013

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found