This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type PromiseFactory = () => Promise<any>; | |
function promiseAllBatched( | |
promiseFactories: PromiseFactory[], | |
batchSize: number | |
): Promise<any> { | |
let completed = 0; | |
let upNext = 0; | |
let results: any[] = []; | |
const initialBatchSize = Math.min(batchSize, promiseFactories.length); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *range(min, max) { | |
while (min < max) { | |
yield min | |
min += 1 | |
} | |
} | |
// Doesn't run out of memory | |
for (const num of range(1, 1000000000000000000)) { | |
console.log(num) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getBirthday(userid) { | |
let birthday | |
DB.getUser(userid).then(user => { | |
birthday = user.birthday | |
}) | |
return birthday | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const util = require('util'); | |
const S3 = require('aws-sdk/clients/s3'); | |
const client = new S3(); | |
const createBucket = util.promisify(client.createBucket); | |
const upload = util.promisify(client.upload); | |
const getObject = util.promisify(client.getObject); | |
client.createBucket = createBucket; | |
client.upload = upload; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const test = { | |
spy: function(fn) { | |
const calls = []; | |
return new Proxy(stuff, { | |
apply(target, thisArg, args) { | |
const result = target.apply(thisArg, args); | |
calls.push([args, result]); | |
}, | |
get(target, p){ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const myObject = {}; | |
const loudObject = new Proxy({}, { | |
get(target, p) { | |
console.log(`Accessing key ${String(p)} at ${(new Error()).stack}`); | |
return target[p]; | |
}, | |
set(target, p, value) { | |
console.log(`Setting key ${String(p)} to ${String(value)} at ${(new Error()).stack}`); | |
target[p] = value; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function *sortOfAsyncFunction() { | |
const ivysaur = yield fetch('https://pokeapi.co/api/v2/pokemon/ivysaur'); | |
return ivysaur.name.toUpperCase(); | |
} | |
// "IVYSAUR" | |
runner(sortOfAsyncFunction).then(result => console.log(result)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function runner(gen, ...args) { | |
// Initialize the generator. | |
let iterator = gen(...args); | |
// handleNext is responsible for restarting the generator with new input. | |
return Promise.resolve().then(function handleNext(value) { | |
// next can be a promise or a sync value. | |
let next = iterator.next(value); | |
// handleResult is responsible for taking the output of the generator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
async function doStuff() { | |
const partyProfileRes = fetch('/v1/users'); | |
const accountRes = fetch('/v1/customers'); | |
const [partyProfile, account] = await Promise.all([ | |
partyProfileRes, | |
accountRes | |
]); | |
try { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getGuides() { | |
let ids = [...Array(10).keys()]; | |
return ids.reduce((accumulator, id) => { | |
return accumulator.then(results => { | |
return fetch(id).then(result => { | |
results.push(result); | |
return results; | |
}); | |
}); |
NewerOlder