Posts

No promises: asynchronous JavaScript with only generators

Two ECMAScript 6 [1] features enable an intriguing new style of asynchronous JavaScript code: promises [2] and generators [3] . This blog post explains this new style and presents a way of using it without promises. Overview Normally, you make a function calls like this: let result = func(···); console.log(result); It would be great if this style of invocation also worked for functions that perform tasks (such as downloading a file) asynchronously. For that to work, execution of the previous code would have to pause until func() returns with a result. Before ECMAScript 6, you couldn’t pause and resume the execution of code, but you could simulate it, by putting console.log(result) into a callback, a so-called continuation [4] . The continuation is triggered by asyncFunc() , once it is done: asyncFunc('http://example.com', result => { console.log(result); }); Promises [2] are basically a smarter way of managing callbacks: asyncFunc('http...

Aggiornamento

Image
The sky is the most amazing cerulean blue.   Families lounge on blankets listening to the lyrical passions of the Mariachi music.   There is the flash of color and brilliant white of the Ballet Folklorico dancers.   Away from the stage, children create chalk designs on the sidewalk leading to the mansion, a place steeped in the history of El Pueblo de Los Angeles .   There is the smell of tacos in the air, a gustatory salute to the street cuisine of the city.   Through a shaded door into a quiet room, pilgrims make their way in reflective splendor around a labyrinth drawn on a cloth and laid out on the floor.   Back out in the sunlight, there are voices and languages under a canopy of trees.   In the distance is the dome of St. Vincent de Paul’s Church , a Los Angeles cultural monument and the second Roman Catholic house of worship to be consecrated in the city.   On this perfect spring day, every act is a prayer, every word contains a universe o...

Iterables and iterators in ECMAScript 6

Image
This blog post is outdated. Please read chapter “ Iterables and iterators ” in “Exploring ES6”. This blog post is part of a series on iteration in ES6: Iterables and iterators in ECMAScript 6 ES6 generators in depth ECMAScript 6 introduces a new interface for iteration, Iterable . This blog post explains how it works, which language constructs consume data via it (e.g., the new for-of loop) and which sources provide data via it (e.g., arrays). Iterability The idea of iterability is as follows. Data consumers: JavaScript has language constructs that consume data. For example, for-of loops over values and the spread operator ( ... ) inserts values into arrays or function calls. Data sources: The data consumers could get their values from a variety of sources. For example, you may want to iterate over the elements of an array, the key-value entries in a map or the characters of a string. It’s not practical for every consumer to support all sources, especially because it should be pos...

A 90 minute overview of ECMAScript 6 (video)

On February 1, 2015, I held the 90 minute talk “Using ECMAScript 6 today” at the Rolling Scopes Conference in Minsk. A video recording of that talk is online: Part 1 [40:44] Part 2 [53:04] Slides

Vote for your favorite “Exploring ES6” cover!

There are two candidates for the cover of my upcoming book, “ Exploring ES6 ”. You can now vote for your favorite . I’ll announce a winner in a week.

Classes in ECMAScript 6 (final semantics)

Image
Check out my book (free online): “ Exploring ES6 ”. Updated version of this blog post: chapter “ Classes ”. Recently , TC39 decided on the final semantics of classes in ECMAScript 6 [2] . This blog post explains how their final incarnation works. The most significant recent changes were related to how subclassing is handled. Overview class Point { constructor(x, y) { this.x = x; this.y = y; } toString() { return '(' + this.x + ', ' + this.y + ')'; } } class ColorPoint extends Point { constructor(x, y, color) { super(x, y); this.color = color; } toString() { return super.toString() + ' in ' + this.color; } } let cp = new ColorPoint(25, 8, 'green'); cp.toString(); // '(25, 8) in green' console.log(cp instanceof ColorPoint); // true console.log(cp instanceof Point...