Skip to content

Instantly share code, notes, and snippets.

View anonimusprogramus's full-sized avatar

Anonimus Programus anonimusprogramus

View GitHub Profile

Do not use forEach with async-await

TLDR: Use for...of instead of forEach in asynchronous code.

The problem

Array.prototype.forEach is not designed for asynchronous code. (It was not suitable for promises, and it is not suitable for async-await.)

For example, the following forEach loop might not do what it appears to do:

@anonimusprogramus
anonimusprogramus / terser.js
Created October 11, 2022 09:46 — forked from jrschumacher/terser.js
A terser script to minify all javascript files in a directory
const fs = require('fs')
const {sync: globSync} = require('glob')
const filesize = require('filesize')
const Terser = require('terser')
const options = require(process.env.TERSER_CONFIG || './terserrc.json')
const getSize = (file) => {
const {size} = fs.statSync(file)
return filesize(size)
}