Comments (9)
Hello Brian,
I agree valueOf()
may lead to confusion, misunderstanding and misuse.
IMHO, instead of or along with valueOf
it would be useful to have special API to check resolution status and get result. Something like the following:
isResolved()
: returnstrue
if promise is already resolved.getResult()
orgetValue()
: returns the resolved value if promise is resolved orundefined
if it is not.
I suppose toString()
implementation might be useful, at least for debug purposes. But I think for resolved promise it would be better to return another string that includes "class" and "properties", for example: [object Promise(resolved;<result>)]
Denis
from when.
Hey Denis, thanks for the feedback.
I've know that there are other promise libraries, like Q and jQuery, that provide isResolved()
and getResult()
type functions. I have the same concerns about those, because I think they are essentially the same as valueOf()
. Anything that encourages a developer to poll for promise resolution seems bad to me. For example, you could write the same setInterval
nonsense using isResolved()
or getResult()
as you could with valueOf()
. I think when.js should encourage (force?) people use when()
and/or .then()
.
That said, maybe there are valid use cases for isResolved()
and getResult()
. If you have some in mind, let's discuss them here, since that would definitely help with making a decision!
Yep, I agree that toString()
could be useful for debugging, and that might be the compelling reason to implement it. Developers could still misuse toString()
to poll for resolution, but I think that's less likely, and it doesn't directly encourage polling, like valueOf()
, isResolved()
, and getResult()
would.
from when.
Brian,
I agree that polling of promise resolution is bad idea and developers should use when().then()
approach with proper callbacks. At the moment, I don't have any use cases for isResolved()+getResult()
or valueOf()
.
from when.
Cool, thanks for the info, Denis!
from when.
Ok, I'm taking valueOf()
off the table for the reasons noted in the discussion above--basically that it encourages polling instead of using when()
or .then()
. I think toString()
could still be reasonable, but now that when.js promises share a constructor, it's pretty obvious in any reasonable debugger that an object is a promise. So, I don't see this as a high priority for now ... I'll probably let it ride until we find a convincing use case.
from when.
Brian,
Some environments don’t have reasonable debuggers. ;-)
For example, I develop for devices where I can’t use any JS debugger so I have to utilize logging for bugs catching. Having proper toString
implementation would be helpful in similar situations.
from when.
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 Error
that indicates it timed out:
// 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 toString()
would still provide some benefit? If so, do you think it should:
- return "[object Promise]", or
- return "[object Promise + indication of state: pending, resolved, rejected]", or
- return "[object Promise + indication of state plus resolution value or rejection reason]"
- Something else?
Thanks!
from when.
Brian,
Thank you for hints and the code. Certainly, this is very helpful and can be used for logging and debugging.
But IMHO providing toString()
to specific classes and objects is a good manner, especially, for library.
As for concrete implementation I would prefer the third variant.
from when.
There will be a way to enable a toString()
method, in addition to a few other debug features, on promises, deferreds, and resolvers in the upcoming v1.1.0.
from when.
Related Issues (20)
- JSPM build (SystemJS builder) failing on [email protected] HOT 3
- Does not work in NodeJS (ReactJS Native) HOT 12
- Visual Studio debugger breakes from adding When to WebPack bundle. HOT 13
- 3.7.7 breakes the build on Windows HOT 2
- no such file or directory .... es6-shim/makePromise HOT 2
- Global rejection events completely broken in a bundled environment (eg. Webpack)
- When not working in Jasmine tests
- when.reduce UnhandledPromiseRejectionWarning HOT 4
- Misleading unhandled rejection warning when using when.settle HOT 10
- npm install ! has errors HOT 1
- Usage hello world example fails with TypeError HOT 3
- Binding context to promise chain HOT 2
- Add react-native support HOT 2
- File 404 when try to use in browser environment
- Promise.js first function never runs HOT 2
- RTE TypeError after production build HOT 4
- Does function call also accept promises? HOT 4
- Current project status HOT 1
- npm install failing with 404 error code
- Mark package as deprecated on npm?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from when.