Posts

Bill Cunningham

Image
Photo courtesy of Lenzartis One of my heroes passed last week.   Bill Cunningham first drew my attention when I saw a documentary about him.   He was a tiny wisp of a man, always with a blue smock jacket, his bicycle, and his camera as he raced through the streets of midtown Manhattan photographing street fashions for his weekly spread in The New York Times .   At night, he hovered and flitted through the summer garden parties of the rich and famous to document the gatherings in a second collection prominent in the paper’s Sunday Style section.   At 87, he seemed indestructible, that he would go on for ages and ages even though he appeared delicate and child-like.   No one does the kind of work Bill did.   He was an artist living an ascetic life.   His photography was everything to him.   I admired his singular focus, his obsession with his art.   Like most true artists, he would continue even if no one paid him.   In fact, there were...

Taking a break

For health reasons, I’m taking June–August off from work (Twitter, blogging, etc.). See you in September! To tide you over, you can read (and buy, to support my work) my books, which are free to read online: Speaking JavaScript Exploring ES6 Setting up ES6 Exploring ES2016 and ES2017

Six nifty ES6 tricks

Image
In this blog post, I show six tricks enabled by new ES6 features. At the end of each section, I point to related material in my book “ Exploring ES6 ” (which is free to read online). Enforcing mandatory parameters via parameter default values ES6 parameter default values are only evaluated when they are actually used. That lets you enforce that a given parameter be provided: /** * Called if a parameter is missing and * the default value is evaluated. */ function mandatory() { throw new Error('Missing parameter'); } function foo(mustBeProvided = mandatory()) { return mustBeProvided; } The function call mandatory() is only made if the parameter mustBeProvided is missing. Interaction: > foo() Error: Missing parameter > foo(123) 123 More information: Sect. “ Required parameters ” in “Exploring ES6” Iterating over Array indices and elements via the for-of loop Method forEach() lets you iterate over the elem...

Handling whitespace in ES6 template literals

In this blog post, we look at problems that arise when template literals contain whitespace: Breaking up long lines Dedenting content Joining Arrays Indenting inserted content I’m using the library common-tags by Declan de Wet (with “useful template literal tags for dealing with strings in ES6”) to demonstrate solutions for some of these problems. Breaking up long lines Occasionally, you have long lines that you want to break up. common-tag’s tag function oneLine lets you do that: console.log(oneLine` a single line with many words `); // a single line with many words Dedenting content Template literals let you embed multi-line text content inside JavaScript. The main challenge is that the text must both have proper indentation and fit nicely into its JavaScript surroundings: function foo() { console.log(`<ul> <li>first</li> <li>second</li> </ul>`); } This does not look good...

Trees of Promises in ES6

This blog post shows how to handle trees of ES6 Promises , via an example where the contents of a directory are listed asynchronously. The challenge We’d like to implement a Promise-based asynchronous function listFile(dir) whose result is an Array with the paths of the files in the directory dir . As an example, consider the following invocation: listFiles('/tmp/dir') .then(files => { console.log(files.join('\n')); }); One possible output is: /tmp/dir/bar.txt /tmp/dir/foo.txt /tmp/dir/subdir/baz.txt The solution For our solution, we create Promise-based versions of the two Node.js functions fs.readdir() and fs.stat() : readdirAsync(dirpath) : Promise<Array<string>> statAsync(filepath) : Promise<Stats> We do so via the library function denodify : import denodeify from 'denodeify'; import {readdir,stat} from 'fs'; const readdirAsync = denodeify(readdir); const statAsync = d...

Tracking unhandled rejected Promises

In Promise-based asynchronous code, rejections are used for error handling. One risk is that rejections may get lost, leading to silent failures. For example: function main() { asyncFunc() .then(···) .then(() => console.log('Done!')); } If asyncFunc() rejects the Promise it returns then that rejection will never be handled anywhere. Let’s look at how you can track unhandled rejections in browsers and in Node.js. Unhandled rejections in browsers Some browsers (only Chrome at the moment) report unhandled rejections. unhandledrejection Before a rejection is reported, an event is dispatched that you can listen to: window.addEventListener('unhandledrejection', event => ···); The event is an instance of PromiseRejectionEvent whose two most important properties are: promise : the Promise that was rejected reason : the value with which the Promise was rejected The following example demonstrates how this event works: window.addEven...

At The Existentialist Cafe

Image
“To philosophize is to learn how to die.” (Cicero) There are books we remember all our lives.   We remember where we first read them, the way they made us feel, the way the world seemed so fresh, so colorful, so new when we finished the last page.   We remember how we never wanted them to end. I had that experience reading Sarah Bakewell’s At The Existentialist Café:  Freedom, Being and Apricot Cocktails (Other Press, 2016).   I knew Bakewell was good— I reviewed her book on Montaigne a few years ago—but this is a masterwork of biography not just of a person, but people, places, a philosophy and a way of life.   In fact, early on, she calls Existentialism more a mood than a philosophy and traces its lineage back to Job and Ecclesiastes in the Bible, up through St. Augustine and Blaise Pascal, to Jean-Paul Sartre, Simone de Beauvoir, Albert Camus , Martin Heidegger, Edmund Husserl, Karl Jaspers, and Maurice Merleau-Ponty.   The last seven names are the ...