Skip to content

Instantly share code, notes, and snippets.

View schahriar's full-sized avatar

Schahriar SaffarShargh schahriar

  • San Francisco, United States
View GitHub Profile
@schahriar
schahriar / _readme.md
Created April 17, 2022 20:47 — forked from steeve/_readme.md
How to cross compile Go with CGO programs for a different OS/Arch

How to cross compile Go with CGO programs for a different OS/Arch

It is possible to compile Go programs for a different OS, even though go build says otherwise.

You'll need:

@schahriar
schahriar / awaiting-a-race.js
Created January 15, 2018 09:00
In-flight promises awaiting a race #async-await #medium
while(inFlight.size >= limit) {
await Promise.race(inFlight);
}
@schahriar
schahriar / In-flight-promises.js
Created January 15, 2018 08:57
In-flight promises #async-await #medium
async (promises) => {
let inFlight = new Set();
return promises.map((promise) => {
// Add promise to inFlight Set
inFlight.add(promise);
// Delete promise from Set when it is done
promise.then(() => inFlight.delete(promise));
});
}
@schahriar
schahriar / async-race.js
Created January 15, 2018 05:25
Limits race #async-await #medium
const randAsyncTimer = (i) => {
// Timeout within 1 second
const timeout = Math.floor(Math.random() * 1000);
return new Promise((resolve) => setTimeout(() => resolve(i), timeout));
};
async () => {
let calls = [randAsyncTimer(1), randAsyncTimer(2), randAsyncTimer(3)];
// Start the race
const result = await Promise.race(calls);
@schahriar
schahriar / c-while-loop.js
Created January 14, 2018 22:23
Conditional while loop #async-await #medium
const runFor = async (time, func, interval) => {
// Runs an interval until time is past
while (time > Date.now()) {
await timeoutPromise(interval);
// Note that you'll need to account
// for func execution time if it is
// async
func();
}
};
@schahriar
schahriar / delayed-loop.js
Created January 14, 2018 22:17
Delayed loop #async-await #medium
const randForTen = async () => {
let results = [];
for (let i = 0; i < 10; i++) {
await timeoutPromise(1000);
results.push(Math.random());
}
return results;
}
@schahriar
schahriar / timers-between-functions.js
Created January 14, 2018 22:02
Control flow timers in action #async-await #medium
const z = async () => {
await x();
await timeoutPromise(1000); // Wait 1 second
// You can recurse z if needed
return y();
}
@schahriar
schahriar / async-timers.js
Created January 14, 2018 21:52
Control flow timers #async-await #medium
const immediatePromise = () => new Promise((resolve) => setImmediate(resolve));
const timeoutPromise = (timeout) => new Promise((resolve) => setTimeout(resolve, timeout));
@schahriar
schahriar / cf-parallel.js
Created January 14, 2018 11:02
Control flow parallel #async-await #medium
const [user1, user2] = await Promise.all([db.get('user1'), db.get('user2')]);
@schahriar
schahriar / map-parallel.js
Last active January 14, 2018 10:54
Map parallel #async-await #medium
async (items) => {
// Note that async functions return a promise
const promises = items.map(async (item) => {
const result = await db.get(item);
// Log individual results as they finish
console.log(result);
return result;
});
const results = await Promise.all(promises);
console.log(results);