News for hackers. Stuff that matters.
Re:A guide to threads in Node.js by crabasa on Sunday March 24, 2019 @03:38PM (#19478318)
> type WorkerCallback = (err: any, result?: any) => any;

export function runWorker(path: string, cb: WorkerCallback, workerData: object | null = null) { const worker = new Worker(path, { workerData });

worker.on('message', cb.bind(null, null)); worker.on('error', cb);

worker.on('exit', (exitCode) => { if (exitCode === 0) { return null; }

   return cb(new Error(`Worker has stopped with code ${exitCode}`));
 });

 return worker;
}

For an article titled "A Guide to Threads in Node.js", I really wish the first example of writing a thread wasn't in TypeScript.

Re:A guide to threads in Node.js by danenania on Sunday March 24, 2019 @04:32PM (#19478584)
Why? Just think of it as inline documentation. It in no way changes the semantics of the javascript and efficiently communicates how the API works.
Re:A guide to threads in Node.js by crabasa on Sunday March 24, 2019 @04:49PM (#19478679)
When writing a developer guide, especially to something as fundamental as "threads in Node.js", it pays to:

1) Make as few assumptions as possible regarding what the reader knows or has experience with

2) Make code samples as complete and "copy-and-pastable" as possible.

You're assuming that all developers reading this guide will effortlessly translate that TS code to JS.

Using TS creates a possible barrier to understanding, which I'm sure isn't the intent of someone publishing such a guide. Better to simply write some comments or augment the prose in the guide.

Re:A guide to threads in Node.js by danenania on Sunday March 24, 2019 @05:02PM (#19478763)
Ok, fair enough. But for anyone who has some familiarity with type systems, the lack of type signatures (or needing to mentally parse them in some other non-standard format) also acts as a barrier to understanding. Perhaps either a side-by-side view or a tab switcher would be ideal.
Re:A guide to threads in Node.js by crabasa on Sunday March 24, 2019 @05:17PM (#19478827)
Except this is a guide for Node.js (JavaScript). The one assumption the author can make is that the reader knows and is comfortable with JavsScript.
Re:A guide to threads in Node.js by danenania on Sunday March 24, 2019 @05:33PM (#19478911)
Yeah I'm not trying to disagree with you there, just pointing out that TS types are an efficient way to add important context that many (like me) will appreciate, and as a popular well-defined standard I think they are superior to some other kind of ad hoc spec that is included in comments or prose or whatever.

I think it would be great if more JS code examples started including type signatures, since they are useful whether or not you program in TS. But yes, there should probably also be a JS-only example in that case and/or a way to toggle them off.

Re:A guide to threads in Node.js by hackerbob on Sunday March 24, 2019 @07:37PM (#19479385)
We've been throw this game before: CoffeeScript

Until TS becomes an official standard supported by ECMAScript out of the box. Were just going along with what feels or looks good and the javascript community has proven that can change from year to year.

Re:A guide to threads in Node.js by danenania on Sunday March 24, 2019 @08:00PM (#19479454)
Well, I was huge fan of CoffeeScript, so you’ve got me there. I don’t regret a single one of the (many) times I used it though. It made me and the teams I was part of significantly more productive and allowed us to produce more concise and maintainable code than we could have with plain JS. I feel the same about TypeScript today except honestly the productivity gains are a lot more dramatic.
Re:A guide to threads in Node.js by Yaggo on Monday March 25, 2019 @03:39AM (#19480853)
As a former coffeescript fan, doesn’t the extra “clutter” in typescript syntax bother you?

I like CS very much myself. While I can see the benefit of having type system (in certain projects), I just can’t get over the aesthetic/syntax issue.

Re:A guide to threads in Node.js by true_religion on Monday March 25, 2019 @05:03AM (#19481131)
For me, not really. If I want to use type annotation, I need to write the types somewhere in order to communicate it to future readers.

I would much rather do it via Typescripts annotations than JSDocs.

It's not clutter as it was deemed necessary by the writer (me).

If I don't want to use type annotations, I simply don't add them and Typescript does not force you to do so unless you tell it to.

Re:A guide to threads in Node.js by danenania on Monday March 25, 2019 @01:36PM (#19484627)
For me, most of the key CS features that made me reach for it all ended up in ES6. And while I also prefer CS indentation to braces, I don't see that as very important.
Re:A guide to threads in Node.js by jhall1468 on Monday March 25, 2019 @02:08AM (#19480610)
I mean, that requirement (supported by ECMAScript out of the box) means you can't even use babel on the front-end since stage 4 isn't necessarily supported until the next release.

Types aren't coming to ECMAScript anytime soon and with flow and TS it's probably time to get used to the syntax.

Re:A guide to threads in Node.js by kbenson on Sunday March 24, 2019 @04:46PM (#19478663)
Probably because it adds extra cognitive overhead for anyone that doesn't know TypeScript as they interpret and translate TypeScript into JavaScript in their head? Or perhaps because it's slightly misleading?
Re:A guide to threads in Node.js by GordonS on Sunday March 24, 2019 @06:50PM (#19479224)
As a staunch supporter of static typing, I agree with you - the target audience is very obviously those from a JavaScript background, so it makes sense for the code snippets to be in JavaScript, not Typescript. If the author wanted to add additional info for context, IMO in this instance they should have used a JSDoc comment.
Re:A guide to threads in Node.js by hombre_fatal on Sunday March 24, 2019 @09:34PM (#19479736)
Eh, I'd see your point if it was written with ReasonML, but these comments seem ridiculous to me. For example, did you really grapple with the example code? Or are you just assuming everyone is too stupid to understand some inline, ignorable annotations in what remains to be Javascript?
Re:A guide to threads in Node.js by danenania on Sunday March 24, 2019 @08:07PM (#19479473)
TypeScript is a more expressive, concise, intuitive, and popular version of jsdoc that can be statically enforced by a compiler, provide autocompletion, and will never get out of date. I just don’t see the downside of using TS in place of jsdoc—the syntax is even pretty comparable.