News for hackers. Stuff that matters.
Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @01:36PM (#22979707)
Here's my off-the-cuff take that will not be popular.

A function like this should be a package. Or, really, part of standard js, maybe.

A) The problem it solves is real. It's dumb, but JS has tons of dumb stuff, so that changes nothing. Sometimes you want to know "is this thing a promise", and that's not trivial (for reasons).

B) The problem it solves is not straightforward. If you Google around you'll get people saying "Anything with a .then is a promise' or other different ways of testing it. The code being convoluted shows that.

Should this problem be solved elsewhere? Sure, again, JavaScript is bad and no one's on the other side of that argument, but it's what we have. Is "just copy paste a wrong answer from SO and end up with 50 different functions in your codebase to check something" like other languages that make package management hard so much better ? I don't think so.

Re:A one-line package broke `npm create-react-app` by forty on Sunday April 26, 2020 @04:54AM (#22985667)
No. There is no reason why it should be a package by itself. It should be part of a bigger util package, which which is well maintained, tested, and with many maintainers actively looking at it, with good processes, such as systematic code reviews, etc.

At work, our big webapp depended at some point indirectly on "isobject" "isobj" and "is-object", which were all one liners (some of them even had dependencies themselves!!). Please let's all just depend on lodash and it will actually eventually reduce space and bandwith usage.

Re:A one-line package broke `npm create-react-app` by therealdrag0 on Monday April 27, 2020 @12:44PM (#22997483)
Yep, in Java-land this would be in an Apache Common's (or Guava, etc) module with dozens and dozens of other useful functionality.
Re:A one-line package broke `npm create-react-app` by drinchev on Saturday April 25, 2020 @03:21PM (#22980588)
Yeah, but the question is how far should we go with that. Should we do :

    const isFalsy = require("is-falsy");
    const isObject = require("is-object");
    const isFunction = require( "is-function" );
    const hasThen = require( "has-then" );

    function isPromise(obj) {
      return !isFalsy(obj) && ( isObject(obj) || isFunction(obj) ) && hasThen( obj );
    }
Just because the code line is more than 50 characters, doesn't mean that we need a new library for that.
Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @05:40PM (#22981783)
All of those can be pretty much be handled natively, and obviously. They're all primitive

isFalse would be != isObject would use typeOf isFucntion would use typeOf

Where a library becomes helpful is when you have:

* A real problem (none of those are real problems, and the npm packages for them are essentially unused jokes)

* A solution that is not intuitive, or has a sharp edge, or requires non-obvious knowledge, or does not have a preexisting std approach

Checking for a promise, given the constraints of having multiple types of promises out in the world, falls into both of those. Checking if something is falsey, when Javascript provides !, does not fall into either.

Re:A one-line package broke `npm create-react-app` by enitihas on Saturday April 25, 2020 @03:28PM (#22980669)
I think all of the above might already be libraries on npm. From what I remember, npm has isInteger, isPositive, is-odd, is-even.
Re:A one-line package broke `npm create-react-app` by recursive on Saturday April 25, 2020 @03:48PM (#22980831)
All of the packages you mentioned are maintained by the same guy.
Re:A one-line package broke `npm create-react-app` by sergiotapia on Saturday April 25, 2020 @07:59PM (#22982793)
Have you seen his twitter? It's incredibly cringey. I don't understand how someone could be so arrogant to claim millions of companies use his software, when his software is isFalse. Not to mention his hundreds of packages that literally just output an emoji.
Re:A one-line package broke `npm create-react-app` by artificial on Sunday April 26, 2020 @02:15AM (#22984972)
Reminds me of Dr. Evil’s monologue about his Father making ridiculous claims about inventing the question mark.
Re:A one-line package broke `npm create-react-app` by dragonwriter on Saturday April 25, 2020 @03:30PM (#22980681)
isFalsy is just “!”; I don't think we need a new library for a more verbose way to express a one-character unary operator, no, nor does it meet the standard of “The problem it solves is not straightforward” proposed upthread.
Re:A one-line package broke `npm create-react-app` by drinchev on Saturday April 25, 2020 @03:35PM (#22980728)
Re:A one-line package broke `npm create-react-app` by dragonwriter on Saturday April 25, 2020 @04:22PM (#22981122)
I'm not surprised it exists (and literally is just a more verbose, indirect way to invoke “!” that nevertheless is a 17 sloc module with a bunch of ancillary files that has one direct and, by way of that one, 17 second-order and, while I didn't check further, probably an even more ridiculous number of more distant, transitive dependendencies.)

I'm just saying it's neither necessary nor consistent with the standard for when a library is a good idea proposed upthread, so suggesting it as part of an attempt at reductio as absurdam on that standard is misplaced.

Re:A one-line package broke `npm create-react-app` by magicalist on Saturday April 25, 2020 @04:35PM (#22981225)
> 0 Dependents

if a tree falls in the woods...?

Re:A one-line package broke `npm create-react-app` by kemitchell on Saturday April 25, 2020 @08:40PM (#22983078)
> Weekly Downloads > > 0

as of 2020-04-26T00:39+00:00

Re:A one-line package broke `npm create-react-app` by drngdds on Saturday April 25, 2020 @03:35PM (#22980730)
>Or, really, part of standard js, maybe.

I think this would be the solution. I feel like a lot of the NPM transitive dependency explosion just comes from the fact that JavaScript is a language with a ton of warts and a lack of solid built-ins compared to e.g. Python. Python also has packages and dependencies, but the full list of dependencies used by a REST service I run in production (including a web framework and ORM) is a million times smaller than any package-lock.json I've seen.

Re:A one-line package broke `npm create-react-app` by jdminhbg on Saturday April 25, 2020 @04:55PM (#22981409)
This is correct. I post the same thing every time one of these JS dependency hell issues pops up, but it's the case because it's true: The problem is the lack of a standard library. It's not that people don't know how to write a left-pad function, it's that it's dumb to rewrite it in every project and then remember what order you put the arguments in, etc. So people standardize, but they're standardizing on millions of different little packages.

I think the effort that goes into all the JS syntax and module changes would be better put into developing a solid standard library first.

Re:A one-line package broke `npm create-react-app` by galoisgirl on Monday April 27, 2020 @07:12AM (#22994743)
There's a rich standard library, you don't need a package to left-pad.
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.
Re:A one-line package broke `npm create-react-app` by grumple on Saturday April 25, 2020 @05:00PM (#22981439)
This function should absolutely NOT be a package. The problem is that JS has a very minimal standard library and despite tons of money going into the system, nobody's had the good sense to take a leadership role and implement that standard. In other languages you don't need to include external packages to determine the types of objects you're dealing with, or many other things.
Re:A one-line package broke `npm create-react-app` by detaro on Saturday April 25, 2020 @05:01PM (#22981455)
And there's an interesting discussion to be had if it shouldn't be one of those snippets that everyone copies from Stackoverflow instead. And how much trouble in other ways that alternative has caused.
Re:A one-line package broke `npm create-react-app` by jhanschoo on Saturday April 25, 2020 @03:23PM (#22980613)
I don't think it should be a package.

One-liners without dependencies like this should live as a function in a utility file. If justification is needed, there should be a comment with a link to this package's repo.

Re:A one-line package broke `npm create-react-app` by Pfhreak on Saturday April 25, 2020 @03:45PM (#22980804)
What's the difference between a utility file and a package? That seems like a distinction without a difference to me.

If you use the same one liners in more than one project and you copy that utility file over, the line gets even fuzzier.

Re:A one-line package broke `npm create-react-app` by Ndymium on Saturday April 25, 2020 @04:39PM (#22981259)
The utility file will never be updated and break your build without you doing it yourself.
Re:A one-line package broke `npm create-react-app` by hombre_fatal on Saturday April 25, 2020 @09:43PM (#22983500)
Also you can just read the code the same way you read any other code. And since it's in your codebase and git diffs, you will read it.

Because the implementation detail of is-promise actually is important. It just checks if an object has a .then() method. So if you use it, it's just as important that you know the limitation.

Not everything needs to be swept under the rug.

Re:A one-line package broke `npm create-react-app` by darekkay on Sunday April 26, 2020 @05:32AM (#22985839)
Also: The utility file will never be updated and fix existing issues within the utility itself (unless you look up the package and diff it yourself). It's a trade-off.
Re:A one-line package broke `npm create-react-app` by jhanschoo on Sunday April 26, 2020 @07:40AM (#22986404)
As the commenter who suggested keeping it in a utilities file, I'd say that the trade-off is heavily weighted to not importing it as a package.

When you cribbed the code you should have completely understood what exactly the package was doing, and why, and known what issues it would have had. Since it's a one-liner, it is transparent. Since it is without dependencies, it is unlikely to fail on old code. So it's unlikely to have existing issues and unlikely to develop new issues.

Of course, if you end up using new features of the language in your code, it may fail on that, but the risk old stuff failing should have already been factored in when you decided to upgrade. In fact, the one-liner solves this better since you decide the pace of adaptation of your one-liner to the new features, not the package maintainer.

Re:A one-line package broke `npm create-react-app` by darekkay on Sunday April 26, 2020 @08:59AM (#22986830)
That's the trade-off I would most likely take in the "isPromise" case. But the opening question was a generic one ("What's the difference between a utility file and a package"), so the answer should reflect both sides.
Re:A one-line package broke `npm create-react-app` by degraafc on Saturday April 25, 2020 @01:40PM (#22979731)
I'd say that it should rather be a part of the type system. Some kind of `obj isa Promise` should be the way to do this, not random property checks. But that's JS...
Re:A one-line package broke `npm create-react-app` by kroltan on Saturday April 25, 2020 @01:50PM (#22979790)
The thing is that there is the Promise "class", which is provided by the environment, but there is a interface called PromiseLike, which is defined as having a method called then that takes one or two functions. Now, JS doesn't have nominal typing for interfaces, so you have to do "random property checks".

Typescript partially solves that by declaring types, but if you have a any variable, you still need to do some probing to be able to safely convert it to a PromiseLike, because TypeScript goes to great lengths to not actually produce physical code on its output, attempting to be just a type checker.

Perhaps if TS or an extension allowed "materializing" TS, that is `value instanceof SomeInterface` generated code to check for the existence of appropriate interface members, this could be avoided, but alas, this is not the case.

Re:A one-line package broke `npm create-react-app` by ehsankia on Saturday April 25, 2020 @01:59PM (#22979882)
shouldn't it then be called is-promise-like? Also, if you're being loose about it anyways, can't you simply just go for `if (obj && typeof obj.then == 'function')` and call it a day? I'd say that's short enough to include your own version and not rely on a package for.

I think that module over complicates it as it is, and most people don't need that level of complication in their code.

Re:A one-line package broke `npm create-react-app` by zbentley on Saturday April 25, 2020 @03:20PM (#22980586)
> Perhaps if TS or an extension allowed "materializing" TS, that is `value instanceof SomeInterface` generated code to check for the existence of appropriate interface members, this could be avoided

It's not perfect and a bit of a bolt-on, but io.ts works reasonably well in this area:

https://github.com/gcanti/io-ts

Re:A one-line package broke `npm create-react-app` by odensc on Saturday April 25, 2020 @01:43PM (#22979750)
In theory `x instanceof Promise` would work, but the reason for this package is that there are many non-standard Promise implementations in the JS world.
Re:A one-line package broke `npm create-react-app` by exogen on Saturday April 25, 2020 @05:43PM (#22981814)
It wouldn't work even if everything were native – see my reply above.
Re:A one-line package broke `npm create-react-app` by odensc on Saturday April 25, 2020 @07:02PM (#22982421)
That applies for browsers, yes (though I'd argue is a rare edge-case), but create-react-app is a Node.js application.
Re:A one-line package broke `npm create-react-app` by exogen on Saturday April 25, 2020 @08:10PM (#22982869)
create-react-app isn't even using is-promise directly. It's several hops in the dependency graph away.
Re:A one-line package broke `npm create-react-app` by city41 on Saturday April 25, 2020 @03:40PM (#22980762)
Promises were not always part of the standard and for many years were implemented in user space, by many different implementations. Using duck typing like this was the only way to allow packages to interact with each other, as requiring an entire stack to say only use Bluebird promises is not realistic at all.
Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @01:42PM (#22979743)
I'm totally with you on this. It's dumb that this is a problem but it is actually a problem.
Re:A one-line package broke `npm create-react-app` by Thorentis on Sunday April 26, 2020 @12:41AM (#22984493)
I think you're on the right track. We all (I hope) agree that stuff like this should be standardized. But that's not the same as "should be a package".

At the very least, W3, or Mozilla Foundation, or something with some kind of quasi-authority should release a "JS STD" package that contains a whole bunch of helper functions like this. Or maybe a "JS Extras" package, and as function usage is tracked across the eco-system, the most popular/important stuff is considered for addition into the JS standard itself.

Having hundreds of packages that each contain one line functions, simply means that there are hundreds of vectors by which large projects can break. And those can in turn break other projects, etc.

The reason, cynically, that these all exist as separate packages, is because the person who started this fiasco wanted to put a high a download count as possible on his resume for packages he maintains. Splitting everything up into multiple packages means extra-cred for doing OSS work. Completely stupid, and I'm annoyed nobody has stepped up with a replacement for all this yet.

Re:A one-line package broke `npm create-react-app` by devit on Saturday April 25, 2020 @03:32PM (#22980699)
A function like this should not be something that anyone even thinks of writing or using.

In properly designed languages, values have either a known concrete type, or the interfaces that they have to support are listed, and the compiler checks them.

Even in JavaScript/TypeScript, if you are using this, you or a library you are using are doing it wrong, since you should know whether a value is a promise or not when writing code.

Re:A one-line package broke `npm create-react-app` by city41 on Saturday April 25, 2020 @03:38PM (#22980744)
This function is most likely an artifact of before promises got standardized. One way promises took off and became so ubiquitous is different implementations could interop seamlessly. And the reason for that is a promise was defined as 'an object or function having a then method which returns a promise when called'.

Doesn't excuse the JS ecosystem and JS as a whole, which truly is a mess. But there's a history behind these things.

Re:A one-line package broke `npm create-react-app` by stefan_ on Saturday April 25, 2020 @04:31PM (#22981198)
I think the point of the comment is: you should not be testing for this at all.

If your API works with promises, call .then() on what is handed to you. That's it. Don't make up emergent, untestable behavior on the spot.

Re:A one-line package broke `npm create-react-app` by city41 on Saturday April 25, 2020 @04:37PM (#22981238)
You need to do this test if you are creating a promise implementation. That was my point, there is a reason code like this exists.
Re:A one-line package broke `npm create-react-app` by earthboundkid on Sunday April 26, 2020 @08:07AM (#22986543)
Why would an implementation need to test for it?

ISTM that a framework may need to test for promiseness if it calls promises and functions differently, but it can and should be done as a utility in the framework, not as a separate package.

Re:A one-line package broke `npm create-react-app` by city41 on Sunday April 26, 2020 @10:22AM (#22987341)
I agree with that. I have no idea why it’s in a separate package. But I can say that about many packages :).

It’s possible to just treat everything as a promise by wrapping results in Promise.resolve() but that can have performance implications that some franeworks might want to avoid by only going down the promise route when they have to.

For promise implementations, If the callback to then() returns a promise, the promise implementation detects that and resolves that promise behind the scenes: http://www.mattgreer.org/articles/promises-in-wicked-detail/...

Re:A one-line package broke `npm create-react-app` by mikewhy on Saturday April 25, 2020 @01:48PM (#22979782)
> Or, really, part of standard js, maybe.see:

It's a part of node, at least: https://nodejs.org/docs/latest-v12.x/api/util.html#util_util...

Re:A one-line package broke `npm create-react-app` by staticassertion on Saturday April 25, 2020 @01:50PM (#22979796)
This will work for a standard Promise, which is great, but not for weirdo made up promises. It also was released, I think, in 2018.

It's one thing if you own the entire codebase, but if you're building a popular, multiple-years-old library/framework, you can't make the same assumptions.

Re:A one-line package broke `npm create-react-app` by mikewhy on Saturday April 25, 2020 @02:23PM (#22980066)
I'm only finding about it today TBH. Usually I go for what this library does:

  function isPromise(obj) {
    return typeof obj?.then === 'function'
  }
Re:A one-line package broke `npm create-react-app` by wolco on Saturday April 25, 2020 @10:38PM (#22983862)
Shouldn't a js framework exist that includes these static basic if checks in the core and offers them as a buildin method? Why load this as an external package, why not copy the code and maintain locally?