Is there a way to short circuit async/await flow?

The standard way to do this now is through AbortSignals async function update({ signal } = {}) { // pass these to methods to cancel them internally in turn // this is implemented throughout Node.js and most of the web platform try { var urls = await getCdnUrls({ signal }); var metadata = await fetchMetaData(urls); … Read more

Error: Missing class properties transform

You need to install @babel/plugin-proposal-class-properties: npm install @babel/plugin-proposal-class-properties –save-dev or yarn add @babel/plugin-proposal-class-properties –dev and add the following to your Babel configuration file – usually .babelrc or babel.config.js. “plugins”: [“@babel/plugin-proposal-class-properties”],

Difference between async/await and ES6 yield with generators

Well, it turns out that there is a very close relationship between async/await and generators. And I believe async/await will always be built on generators. If you look at the way Babel transpiles async/await: Babel takes this: this.it(‘is a test’, async function () { const foo = await 3; const bar = await new Promise(resolve … Read more

JavaScript array .reduce with async/await

The problem is that your accumulator values are promises – they’re return values of async functions. To get sequential evaluation (and all but the last iteration to be awaited at all), you need to use const data = await array.reduce(async (accumP, current, index) => { const accum = await accumP; … }, Promise.resolve(…)); That said, … Read more

JavaScript double colon (bind operator)

No. The bind operator (spec proposal) comes in two flavours: Method extraction ::obj.method ≡ obj.method.bind(obj) “virtual method” calls obj::function ≡ function.bind(obj) obj::function(…) ≡ function.call(obj, …) Neither of them feature partial application. For what you want, you should use an arrow function: (…args) => this.handleStuff(‘stuff’, …args) ≡ this.handleStuff.bind(this, ‘stuff’)

How to know if a function is async?

Theory Native async functions may be identifiable when being converted to strings: asyncFn[Symbol.toStringTag] === ‘AsyncFunction’ Or by AsyncFunction constructor: const AsyncFunction = (async () => {}).constructor; asyncFn instanceof AsyncFunction === true This won’t work with Babel/TypeScript output, because asyncFn is regular function in transpiled code, it is an instance of Function or GeneratorFunction, not AsyncFunction. … Read more

What is ESNext?

What is ESNext, actually? It varies depending on who’s using the term, usually “the next” version of ECMAScript (JavaScript). For instance, when I first wrote this answer in June 2019, if someone said “ESNext” they might be talking about ES2019 plus BigInt, dynamic import, and other features that had recently reached Stage 4 of the … Read more

One-liner to take some properties from object in ES 6

Here’s something slimmer, although it doesn’t avoid repeating the list of fields. It uses “parameter destructuring” to avoid the need for the v parameter. ({id, title}) => ({id, title}) (See a runnable example in this other answer). @EthanBrown’s solution is more general. Here is a more idiomatic version of it which uses Object.assign, and computed … Read more