News for hackers. Stuff that matters.
Re:A one-line package broke `npm create-react-app` by alerighi on Saturday April 25, 2020 @02:30PM (#22980124)
Lile...

    x instanceof Promise 
It works for standard promises, sure there are non standard promises, ancient stuff, that to me shouldn't be used (and a library that uses them should be avoided). So why you need that code in the first place?

Also that isPromise function will not work with TypeScript, imagine you have a function that takes something that can be a promise or not (and this is also bad design in the first place), but then you want to check if the argument is a Promise, sure with `instanceof` the compiler knows that you are doing, otherwise not.

Also, look at the repo, a ton of files for a 1 line function? Really? You take less time to write that function yourself than to include that library. But you shouldn't have to write that function in the first place.

Re:A one-line package broke `npm create-react-app` by exogen on Saturday April 25, 2020 @05:41PM (#22981795)
Your implementation is broken even if everything uses native Promises. I don't know how many times this exact thread needs to happen on HN (as it has many times before) until people realize their "no duh" implementations of things are actually worse than the thing they're criticizing.

Make an iframe.

In the iframe:

    > window.p = new Promise(() => {});
From the parent window:

    > window.frames[0].p instanceof Promise
    false
Congrats! Your isPromise function was given a Promise and returned the incorrect result. The library returns the correct result. Try again!
Re:A one-line package broke `npm create-react-app` by suyjuris on Saturday April 25, 2020 @06:33PM (#22982204)
In case someone else is also confused by this, it seems that instanceof checks whether the objects prototype matches, and these prototypes are not shared across different contexts, which iframes are [0]. (Though I would still like to know why it works like this.)

[0] https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re:A one-line package broke `npm create-react-app` by odensc on Saturday April 25, 2020 @07:03PM (#22982427)
For security reasons - you can modify the prototypes and you wouldn't want iframes to inherit that.
Re:A one-line package broke `npm create-react-app` by stagas on Sunday April 26, 2020 @02:24AM (#22985002)
No, it was not given a Promise. It was given a foreign object from another window. If you want to inspect another window you should not be reusing code that is designed for single threaded operations. Instead, have a layer that translates, serializes, or explicitly defines an interface that the objects we are dealing with are foreign and need to be transformed. Then the abstraction implementation details of dealing with multiple windows become a concern of a single layer and not your entire codebase. Implicitly and magically treating a foreign window as this window, will fail in many subtle and unknown ways. The "brokenness" you mention is not in that implementation, it is correctly breaking, telling you that what you are doing is wrong, then you try to bypass the error instead of fixing your approach.
Re:A one-line package broke `npm create-react-app` by exogen on Sunday April 26, 2020 @01:10PM (#22988504)
For foreign-origin iframes, that's exactly what people do using `postMessage`. But for same-origin iframes there's no need since you can access the iframe's context directly. So people can (and do) write code exactly like this that accesses data directly.

And it was given a Promise. You just shouldn't use instanceof in multi-window contexts in JavaScript. This is why built-ins like `Array.isArray` exist and should be used instead of `arr instanceof Array`. Maybe you'd prefer to write to TC39 and tell them that `Array.isArray` is wrong and should return false for arrays from other contexts?

There's no use jumping through hoops to avoid admitting that OP made an error. They were wrong and didn't think of this.

Re:A one-line package broke `npm create-react-app` by foepys on Sunday April 26, 2020 @03:02AM (#22985171)
GP's comment screams XY problem which seem to be increasingly common these days.
Re:A one-line package broke `npm create-react-app` by exogen on Sunday April 26, 2020 @01:44PM (#22988732)
If you think pointing out a bug due to an edge case someone didn't think of is the XY problem, I'm afraid you don't know what the XY problem is.
Re:A one-line package broke `npm create-react-app` by foepys on Sunday April 26, 2020 @03:42PM (#22989601)
The problem was to get the promise out of the iframe when you shouldn't do this directly in the first place.

This literally is an XY problem: "I need to do A but it's giving me bad results, what do I need to add?" - "Don't use A, it's bad practice. Use B instead and keep using built-in tools instead of hacking something together" In this case use instanceof instead of is-promise because it's a hack around the actual problem of getting objects out of a different context that was explicitly designed to behave this way.

I'm afraid that you don't know what an XY problem is.

JavaScript developers always seem to think they are the smart ones after their 6 weeks of some random bootcamp and then you end up with some crap like NPM where a single line in a package out of hundreds maintained by amateurs can break everybody's development environment.

[dead]
Re:A one-line package broke `npm create-react-app` by dang on Tuesday April 28, 2020 @01:26AM (#23003696)
Yikes, please don't break the site guidelines like this.

https://news.ycombinator.com/newsguidelines.html

Re:A one-line package broke `npm create-react-app` by foepys on Monday April 27, 2020 @01:01AM (#22993018)
I didn't even mean you but the general JS community but if you want to think I did, okay, feel free to do so.
Re:A one-line package broke `npm create-react-app` by hombre_fatal on Saturday April 25, 2020 @09:46PM (#22983520)
Though, let's also appreciate just how niche that case is. I'd be surprised if more than 0.5% of the JS devs reading this will ever encounter that scenario where they are reaching across VMs like that in their life.

`obj instanceof Promise` and `typeof obj.then === 'function'` (is-promise) are much different checks. Frankly, I don't think either belongs in a library. You should just write that code yourself and ponder the trade-offs. Do you really just want to check if an object has a then() method or do you want to check its prototype chain?

Re:A one-line package broke `npm create-react-app` by __s on Saturday April 25, 2020 @03:10PM (#22980504)
TypeScript supports 'function f(x: any): x is T' as a way to declare that if f returns true, x may pass as type T

https://www.typescriptlang.org/docs/handbook/advanced-types....

Re:A one-line package broke `npm create-react-app` by cyral on Saturday April 25, 2020 @10:15PM (#22983722)
This package has an index.d.ts file that utilizes exactly this.
Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @02:43PM (#22980263)
1) I'm not defending the implementation of is-promise. I don't care to, I'm not a javascript developer.

2) > sure there are non standard promises, ancient stuff, that to me shouldn't be used

If you're building a library, or maintaining one that's been built over many years, you can't easily make calls like that.

Re:A one-line package broke `npm create-react-app` by dragonwriter on Saturday April 25, 2020 @03:26PM (#22980649)
> If you're building a library, or maintaining one that's been built over many years, you can't easily make calls like that.

Well, you can, and in the JS ecosystem you'll often find cases where there are two libraries (or two broad classes of libraries) for a certain function that make different choices, one of which makes the simple, modern choice that doesn't support legacy, and one that does the complex, messy thing necessary to deal with legacy code, and which you use depends on your project and it's other constraints.

Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @05:42PM (#22981807)
OK, then the legacy library can't easily make that choice. I'm not saying every single javascript developer should be accepting async or sync callbacks, just that some libraries are choosing to do that for legitimate reasons.
Re:A one-line package broke `npm create-react-app` by cyral on Saturday April 25, 2020 @10:14PM (#22983715)
Are you saying that this isPromise package will not play well with TypeScript? One of those files (index.d.ts) solves the TypeScript problem using type predicates. TypeScript WILL know that the object is a promise if it returns true.