Skip to content

Instantly share code, notes, and snippets.

View gwilczynski's full-sized avatar

Grzegorz Wilczyński gwilczynski

View GitHub Profile
@gwilczynski
gwilczynski / audit.yml
Last active March 17, 2024 21:58 — forked from LukeMathWalker/audit.yml
GitHub Actions - Rust setup
name: Security audit
on:
schedule:
- cron: '0 0 * * *'
push:
paths:
- '**/Cargo.toml'
- '**/Cargo.lock'
jobs:
security_audit:

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:

@gwilczynski
gwilczynski / do_something.js
Created January 21, 2020 09:11
Expected number of calls: >= 1 Received number of calls: 0
import axios from "axios";
const getSomething = (url, onSucces, onError) => {
axios
.get(url)
.then(result => {
onSucces(result.data.results);
})
.catch(error => {
onError(error);
@gwilczynski
gwilczynski / benchmark.rb
Created March 2, 2017 23:23
Please don’t hate OpenStruct
require 'benchmark'
require 'benchmark/ips'
require 'ostruct'
require 'active_model'
BillingAddressStruct = Struct.new(:street, :city, :zipcode, :country, :state)
BillingAddressOpenStruct = OpenStruct
BillingAddressStructFromHash = Struct.new(:street, :city, :zipcode, :country, :state) do
@gwilczynski
gwilczynski / date_parser.rb
Last active December 3, 2015 11:12
Date parser
require 'date'
require 'benchmark'
def real_time
Benchmark.realtime do
100000.times do
yield
end
end
end