Are ‘Arrow Functions’ and ‘Functions’ equivalent / interchangeable?

tl;dr: No! Arrow functions and function declarations / expressions are not equivalent and cannot be replaced blindly. If the function you want to replace does not use this, arguments and is not called with new, then yes. As so often: it depends. Arrow functions have different behavior than function declarations / expressions, so let’s have … Read more

Call async/await functions in parallel

You can await on Promise.all(): await Promise.all([someCall(), anotherCall()]); To store the results: let [someResult, anotherResult] = await Promise.all([someCall(), anotherCall()]); Note that Promise.all fails fast, which means that as soon as one of the promises supplied to it rejects, then the entire thing rejects. const happy = (v, ms) => new Promise((resolve) => setTimeout(() => resolve(v), … Read more

What is “export default” in JavaScript?

It’s part of the ES6 module system, described here. There is a helpful example in that documentation, also: If a module defines a default export: // foo.js export default function() { console.log(“hello!”) } then you can import that default export by omitting the curly braces: import foo from “foo”; foo(); // hello! Update: As of … Read more

Using Node.js require vs. ES6 import/export

Update Since Node v12 (April 2019), support for ES modules is enabled by default, and since Node v15 (October 2020) it’s stable (see here). Files including node modules must either end in .mjs or the nearest package.json file must contain “type”: “module”. The Node documentation has a ton more information, also about interop between CommonJS … Read more

What is the difference between “let” and “var”?

Scoping rules The main difference is scoping rules. Variables declared by var keyword are scoped to the immediate function body (hence the function scope) while let variables are scoped to the immediate enclosing block denoted by { } (hence the block scope). function run() { var foo = “Foo”; let bar = “Bar”; console.log(foo, bar); … Read more