14 ms·
Wow just wow. So here's your new Promise object: class World { then () { return 0; } } isPromise(new World) // true If there really isn't a safe and better
by 2ion 6y ago
Wow just wow. So here's your new Promise object:
class World {
then () { return 0; }
}
isPromise(new World) // true
If there really isn't a safe and better way to tell if an object is an instance of Promise…then color me impressed.
- chrisoverzero 6y agoAccording this this library, maybe. According to the specification, no: https://promisesaplus.com/ https://promisesaplus.com/
- TurplePurtle 6y agoThere are custom Promise implementations (for reasons), such as bluebird.js. If you're supporting legacy browsers, there will be no standard Promise object. So the simplest way to check for the Promise contract is the code posted. But yes, in an ideal world, one would be able to just do `promise instanceof Promise`.
- stagas 6y agoThese cases should really be handled at the compilation/transpilation level, since there is one, and users should just write latest generation JavaScript without these concerns.
- ehsankia 6y agoOr just: const p = {then: () => 0}
- hombre_fatal 6y agoWell, an object with a then() method is a promise. Promise.resolve({then: () => console.log('called')}) Promises autoflatten since you can't have Promise<Promise<T>>, so you'll see that this code prints 'called'.
- throwanem 6y agoI mean, if you have to assume deliberately adversarial action on the part of your own codebase, you may have worse problems than having to duck-type promises.
- gpm 6y agoA class with a `then` method isn't a rare thing that could only come up adversarially. Below I've linked two example from the rust std-library (I just chose that language because the documentation is really easy to search for objects which have a method name then). I think we can be sure that both booleans, and the "less than, equal to, or greater than" enum are not in fact promises. https://doc.rust-lang.org/std/primitive.bool.html#method.then https://doc.rust-lang.org/std/primitive.bool.html#method.the... https://doc.rust-lang.org/std/cmp/enum.Ordering.html#method.then https://doc.rust-lang.org/std/cmp/enum.Ordering.html#method....
- throwanem 6y agoWhat do Rust idioms have to do with Javascript?
- gpm 6y agoThe language has nothing to do with it. The point is just that the name "then" is a perfectly common method name. If a small standard library is using it for things that aren't promises, you can bet your ass that there are javascript libraries using it for things that aren't promises. Like I said, I just chose to look at rust first because it's documentation has a good search bar.
- throwanem 6y agoThe language has everything to do with it, because the language is the locus of practice. Rust practice is whatever it is, and is apparently pretty free with the use of "then" as a method name, which is fine. Javascript practice isn't the same as Rust practice, and Javascript practice includes a pretty strong norm around methods named "then". That's why the next time I run into such a method, that doesn't belong to a promise and behave the way a promise's "then" method does, will be the first time I can remember, despite having worked primarily or exclusively in Javascript since well before promises even existed. I'm sure there is an example somewhere on NPM of a wildcat "then", and that if you waste enough of your time you can find it. So what, though? People violate Rust idioms too from time to time, I'm sure. I doubt you'd argue that that calls Rust idioms themselves into question. Why does it do so with Javascript?
- GlitchMr 6y agoThis is a promise as far the language is concerned (and `is-promise` package uses the same definition as the language) - it's sufficient for an value to be an object and to have a `then` property that is callable. For instance, in the following example, the `then` method is being called. (async () => ({ then() { console.log("Called") } }))()
- deleted 6y ago[deleted]
- shp0ngle 6y agoYour code is indeed an example of a promise. A stupid one, since it never “resolves”, but it’s a promise.