News for hackers. Stuff that matters.
Re:Node v8.1.2 by ihsw2 on Thursday June 15, 2017 @09:08AM (#14560001)
Node 8 is the planned LTS release for later this year.

Notably, it brings async/await syntax to vanilla JS. This new feature complements callbacks and largely replaces how Promises are currently consumed (opting for sequential-like execution with try/catch blocks instead of Promise chains).

The closest analogue would be Tasks in C#.

EDIT: removing reference to node-v8 to prevent confusion.

Re:Node v8.1.2 by pier25 on Thursday June 15, 2017 @09:11AM (#14560015)
Aync/await is the best thing since sliced bread. It has made my life so much easier.
Re:Node v8.1.2 by mstade on Thursday June 15, 2017 @10:00AM (#14560411)
I understand the reasons why async/await is designed the way it is, but I still think it is backwards. Really if you wanted to truly make async programming approachable, you should make it transparent. What I mean by this is that instead of marking a function as async, and awaiting values, you'd mark lines as async, opting out of implicit awaits. I wrote up some thoughts on this here:

https://github.com/utilise/emitterify/issues/1#issuecomment-...

The way it is now, I defensively await things all over the place. What's worse, it propagates. If any of your function's dependencies are async, your function will also have to be async (or be ok with continuing operation after it's returned) and it turtles all the way down.

It is nicer than having to deal with promises directly though, there is that.

Re:Node v8.1.2 by maxxxxx on Thursday June 15, 2017 @10:23AM (#14560641)
Agreed. Async is like a virus. Once you start somewhere pretty much everything else must be async too. In C# you can call async code from not async code and then call Task.Wait. Not sure if JS can do that.

But it still beats promises or callback hell.....

Re:Node v8.1.2 by WorldMaker on Thursday June 15, 2017 @11:06AM (#14561059)
A better term than virus is that async is a monad. Welcome to programming with monads. ;)

JS async produces Promises like C# produces Task, so you can call an async function and use the result as a Promise.

Re:Node v8.1.2 by maxxxxx on Thursday June 15, 2017 @11:28AM (#14561282)
Can you wait for a promise synchronously? I am pretty new to JS so please forgive if this is a stupid question.
Re:Node v8.1.2 by WorldMaker on Thursday June 15, 2017 @11:43AM (#14561434)
It's not something JS engines allow today. It's also not something that entirely makes sense from the perspective of a browser threading model or even the libuv-based NodeJS threading model.
Re:Node v8.1.2 by maxxxxx on Thursday June 15, 2017 @01:06PM (#14562207)
I know. We have some wacky use cases where it would make sense. The real answer would probably be to say "Don't use Node for this" but that's a different discussion above my pay grade.
Re:Node v8.1.2 by WorldMaker on Thursday June 15, 2017 @03:06PM (#14563437)
The real answer is probably "those places you think need to synchronous don't need to be synchronous" and you need a refactor or a rearchitecture.
Re:Node v8.1.2 by maxxxxx on Thursday June 15, 2017 @04:24PM (#14564049)
Trust me. We are not totally stupid. We need synchronous actions under some circumstances.
Re:Node v8.1.2 by pier25 on Thursday June 15, 2017 @11:40AM (#14561395)
> But it still beats promises or callback hell.....

And then() hell ...

Re:Node v8.1.2 by b34r on Thursday June 15, 2017 @10:47AM (#14560878)
You can't really transition between async and sync scopes, no.
Re:Node v8.1.2 by davidgf on Thursday June 15, 2017 @09:31AM (#14560164)
I started using Async/Await and liked it, but I've been learning more about functional programming and went back to Promises
Re:Node v8.1.2 by b34r on Thursday June 15, 2017 @10:48AM (#14560900)
It IS promises ;) labeling a function as async makes it return a promise. await is sugar for avoiding having to roll your own promise tree.
Re:Node v8.1.2 by cloverich on Thursday June 15, 2017 @10:57AM (#14560982)
Sounds interesting -- do you have any resources handy that cover what you allude to (spec. comparing async calls to promises, and favoring the latter?) I use both in my ui code and they seem to have their niche for me. I prefer promise based calls when I want to separate out fetching data from the state-mutating callback, or when I may want to "cancel" something (ex: fetch a list of items for a list component, then unmount the component before the list returns -- no need to handle the response data anymore).
Re:Node v8.1.2 by davidgf on Sunday June 25, 2017 @06:17AM (#14629407)
Unfortunately not, my opinion is simply based on my personal experience and on what I've been learning along the way. I find promises really handy for data processing pipelines (grab some data and perform some transformations over it to get it in the form you need), regardless of wether any of the steps is async or not. I still find async/await really useful and mix it with promises if I need it, but I prefer to keep my code as functional as I can. Besides, I don't really like how JavaScript handles exceptions and would rather staying away from try/catch if possible
Re:Node v8.1.2 by pier25 on Thursday June 15, 2017 @11:34AM (#14561345)
There is no difference, async/await is just another promise notation.
Re:Node v8.1.2 by davidgf on Sunday June 25, 2017 @06:07AM (#14629374)
Although async/await is simply syntax sugar around promises, there's a huge difference indeed. With async/await your code keeps an imperative style, while with promises code end up with a functional look
Re:Node v8.1.2 by almostdigital on Thursday June 15, 2017 @09:16AM (#14560062)
Agreed! once you start using it there's no going back (along with `util.promisify`)
Re:Node v8.1.2 by rockostrich on Thursday June 15, 2017 @09:11AM (#14560013)
It's just Node 8. They stopped prefixing with "v" this version in order to avoid confusion with the V8 javascript engine.
Re:Node v8.1.2 by almostdigital on Thursday June 15, 2017 @09:14AM (#14560041)
But instead they created confusion on how to write the node version number :)
Re:Node v8.1.2 by pdog on Thursday June 15, 2017 @10:22AM (#14560635)
If the goal was to avoid confusion, it was an incredibly dumb decision. I'll continue referring its standard version number: Node v8.
Re:Node v8.1.2 by niklasrde on Thursday June 15, 2017 @09:12AM (#14560019)
Are they still calling it 'v8' instead of 8?

Also notable is that async/await isn't new for node, 7.10 supported it (and previous versions with a flag). It's new for a LTS release. What is new, however, is the in-built util.promisify that can convert callbacks into native promises.

Re:Node v8.1.2 by ihsw2 on Thursday June 15, 2017 @09:14AM (#14560040)
They dropped the "v" from the version name.

Yeah, a lot of companies won't touch non-LTS Node though, so a lot of users won't use async/await until later this year when the next LTS is released (Oct 2017).

Re:Node v8.1.2 by petercooper on Thursday June 15, 2017 @09:50AM (#14560313)
They dropped the "v" from the version name.

Except in their official release blog posts, it seems :-D

Re:Node v8.1.2 by slackingoff2017 on Thursday June 15, 2017 @09:29AM (#14560148)
Promises are and always were retarded. They did nothing to alleviate callback hell which was most of the point of their existence. I have no idea why they implemented them instead of async/await in the first place. Now we have to deal with mixing both for years.

Another fine example of JavaScript trying to be hip and ignoring the lessons learned from countless other languages over the years.

I see this constantly with JS like with the 3+ ways to check for NaN and the object "freeze" system that doesn't actually make objects immutable. They take a solid concept from other languages then put a snazzy spin on it that makes it fucking useless. Then try again in the next version. JS library and syntax is littered with corpses of failed experiments.

At least they finally have a usable ForEach loop after three attempts.

Please typescript save us from this hubris.

Re:Node v8.1.2 by buu700 on Thursday June 15, 2017 @10:06AM (#14560483)
How would you propose that async/await work without Promises? Even C#'s async/await uses the same concept: https://msdn.microsoft.com/en-us/library/system.threading.ta...

Pending the implementation of new language syntax, raw Promises were a practical solution for what they set out to accomplish. If you don't think they improved callback hell at all, I expect that you probably weren't using them correctly (an easy mistake to make without looking at the documentation and/or reading the wrong tutorials / blog posts).

cf.

    getAsyncValue1(a =>
        getAsyncValue2(a, b =>
            getAsyncValue3(b, c =>
                doSomething(c)
            )
        )
    );
and

    getAsyncValue1().then(a =>
        getAsyncValue2(a)
    ).then(b =>
        getAsyncValue3(b)
    ).then(c =>
        doSomething(c)
    );
Sure, async/await flattens that to zero nested callbacks, which is great, but with that off the table I'd rather deal with code flattened to one level deep (or a few, in some edge cases) than arbitrarily many. And that's just the "callback hell" improvement; don't forget what a nightmare error handling with callbacks can be — you have to handle both synchronous exceptions and error value callback parameters, you have to deal with the possibility of the asynchronous function unexpectedly calling your callback more than once, etc.
Re:Node v8.1.2 by Rockslide on Thursday June 15, 2017 @09:47AM (#14560282)
You do realise that async/await is basically syntactic sugar for promises in JS, don't you?
Re:Node v8.1.2 by laggyluke on Thursday June 15, 2017 @09:45AM (#14560262)
You do realize that implementing promises is a prerequisite for async/await, right?
Re:Node v8.1.2 by hderms on Thursday June 15, 2017 @10:00AM (#14560418)
Is it really a requirement? I would think callbacks and a preprocessor are sufficient.
Re:Node v8.1.2 by avaer on Thursday June 15, 2017 @10:10AM (#14560536)
Yes, it's a requirement. If you're using a preprocessor you're not supporting async, you're factoring it out.

Otherwise the argument collapses to: we don't need any of these features, we just need NAND.