DEV Community

Discussion on: What's new and interesting in JavaScript?

Collapse
 
nickytonline profile image
Nick Taylor

Although not in the language yet, I'm excited to see top-level await, pattern matching as well as the null coalesce operator progress and land in the language. Top-level await is already at stage 3, so that is pretty much a shoe in.

Here's the repos for those interested:

GitHub logo tc39 / proposal-top-level-await

top-level `await` proposal for ECMAScript (stage 3)

ECMAScript proposal: Top-level await

Champion: Myles Borins

Status: Stage 3

Synopsis

Top-level await enables modules to act as big async functions: With top-level await, ECMAScript Modules (ESM) can await resources, causing other modules who import them to wait before they start evaluating their body.

Motivation

Limitations on IIAFEs

With await only available within async functions, a module can include an await in the code that executes at startup by factoring that code into an async function:

// awaiting.mjs
import { process } from "./some-module.mjs"
let output
async function main() {
  const dynamic = await import(computedModuleSpecifier)
  const data = await fetch(url);
  output = process(dynamic.default, data);
}
main();
export { output };

This pattern can also be immediately invoked. You could call this an Immediately Invoked Async Function Expression (IIAFE), as a play on IIFE idiom.

// awaiting.mjs
import

GitHub logo tc39 / proposal-pattern-matching

Pattern matching syntax for ECMAScript

ECMAScript Pattern Matching

Status

Stage: 1

Author: Kat Marchán (npm, @maybekatz)

Champions: Brian Terlson (Microsoft, @bterlson), Sebastian Markbåge (Facebook, @sebmarkbage), Kat Marchán (npm, @maybekatz)

Introduction

This proposal adds a pattern matching expression to the language, based on the existing Destructuring Binding Patterns.

There's many proposals potentially related to this one, and other proposals might mention interaction with this. This file includes casual, example-based discussion of the proposal, and there's also a document describing the core semantics in more formal language, which will be iterated over into the final Spec-ese.

There's also a document including suggestions for other future proposals, which are dependent on this one, but do not directly affect the main behavior of the feature.

This proposal was approved for Stage 1 in the May 2018 TC39 meeting, and slides for that presentation are available.

This proposal draws…

GitHub logo tc39 / proposal-nullish-coalescing

Nullish coalescing proposal x ?? y

Nullish Coalescing for JavaScript

Status

Current Stage:

  • Stage 2

Authors

Overview and motivation

When performing optional property access in a nested structure in conjunction with the optional chaining operator, it is often desired to provide a default value if the result of that property access is null or undefined. At present, a typical way to express this intent in JavaScript is by using the || operator.

const response = {
  settings: {
    nullValue: null
    height: 400
    animationDuration: 0
    headerText: ''
    showSplashScreen: false
  }
};

const undefinedValue = response.settings?.undefinedValue || 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue || 'some other default'; // result:
Collapse
 
kenbellows profile image
Ken Bellows

Oh man I didn't know about the pattern matching proposal! I've been waiting for that exact feature in JS ever since I encountered it in Ruby!!! 😍

Collapse
 
toastking profile image
Matt Del Signore

Pattern matching is one of my favorite features of Standard ML and functional languages. Hopefully they add it to Javacript.

Collapse
 
ben profile image
Ben Halpern

Interesting!

Collapse
 
copperwall profile image
Chris Opperwall

Oooo I hadn't heard of top-level await before. That's awesome!