-
Notifications
You must be signed in to change notification settings - Fork 395
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Should when.js Promises implement valueOf() and/or toString()? #21
Comments
Hello Brian, I agree
I suppose Denis |
Hey Denis, thanks for the feedback. I've know that there are other promise libraries, like Q and jQuery, that provide That said, maybe there are valid use cases for Yep, I agree that |
Brian, I agree that polling of promise resolution is bad idea and developers should use |
Cool, thanks for the info, Denis! |
Ok, I'm taking |
Brian, Some environments don’t have reasonable debuggers. ;-) |
That is certainly a great point. I've worked in a similar environment in the past (J2ME), and so I should know better than to assume everyone has access to a reasonable debugger :) One thing I do when I need to resort to console.log or alert for debugging promises is: function logPromise(promise, tag) {
when(promise,
function(value) {
console.log("Promise(" + tag + ") resolved", value);
},
function(err) {
console.error("Promise(" + tag + ") rejected", err);
}
);
} And now that when/timeout is available, it's possible to log a message if a promise doesn't resolve after some reasonable amount of time, which can be some of the most difficult cases to track down. In the case of a timeout, the following will log an // If you're using node or AMD, you can load when/timeout and call it whatever you want, or
// if you're using script tags, load when/timeout, and use the when_timeout global
function logPromise(promise, tag) {
// Setup a *new* promise that will timeout if the provided promise doesn't resolve or
// reject within 5 seconds.
// Note that this *does not* alter the original promise in any way, nor does it cause
// the original promise to reject after 5 seconds.
when_timeout(promise, 5000).then(
function(value) {
console.log("Promise(" + tag + ") resolved", value);
},
function(err) {
console.error("Promise(" + tag + ") rejected", err);
}
);
} That will cover cases where the promise is resolved or rejected explicitly, or doesn't resolve/reject within a particular amount of time. Is that helpful for you? Do you feel like a
Thanks! |
Brian, Thank you for hints and the code. Certainly, this is very helpful and can be used for logging and debugging. |
There will be a way to enable a |
Implementing
valueOf()
could be interesting in that it would allow the valueOf a resolved promise to return the resolution value, which would allow some operators, like ==, +, etc. to work on resolved promises. For example, doing a comparison directly on a promiseif(resolvedPromise == 1) {...}
.The potential downside is that people might misuse this, or if they aren't familiar with using promises, could be confused into using it incorrectly. For example
setInterval(function() { if(promise == 1) doSomething(promise.valueOf()); }, 100)'
, rather thanwhen(promise, doSomething);
Not sure yet what the value would be in implementing
toString()
to return something like"[object Promise]"
when unresolved, andString(resolvedValue)
when it is resolved. But we should at least think through it.The text was updated successfully, but these errors were encountered: