Skip to content

Instantly share code, notes, and snippets.

Avatar

Grzegorz Wilczyński gwilczynski

View GitHub Profile
View async-await-forEach-alternatives.md

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 Jan 21, 2020
Expected number of calls: >= 1 Received number of calls: 0
View do_something.js
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 Mar 2, 2017
Please don’t hate OpenStruct
View benchmark.rb
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
View date_parser.rb
require 'date'
require 'benchmark'
def real_time
Benchmark.realtime do
100000.times do
yield
end
end
end