Skip to content

Instantly share code, notes, and snippets.


Jed Schmidt jed

View GitHub Profile
View iteratePromises.js
// per
// "In JavaScript, is there a function which takes N Promises, and returns an asyncIterator which yields N resolved values?"
let promises = Array.from({length: 10}, (n, i) => {
return new Promise(cb => setTimeout(cb, Math.random() * 1e3, i))
let iterator = iteratePromises(promises)
for await (let value of iterator) console.log(value)
View view-source.js
export default (build, namespace = 'view-source') => ({
name: namespace,
setup({initialOptions, onLoad, onResolve}) {
let options = {...initialOptions, write: false}
let filter = new RegExp(`^${namespace}:`)
onResolve({filter}, ({path, importer}) => {
path = path.replace(filter, '')
try { importer = new URL(importer) }
catch (e) { importer = new URL(`${namespace}://${importer}`) }
jed / tee.js
Last active Nov 24, 2020
Teeing an asynchronous iterator
View tee.js
function tee(asyncIterable) {
let source = asyncIterable[Symbol.asyncIterator]()
return [[], []].map((buffer, i, buffers) => ({
async next() {
if (0 in buffer) return buffer.shift()
let item = await
if (!item.done) buffers[1 - i].push(item)
jed / iterator.js
Last active Jan 21, 2021
Turning callbacks into async iterators, with a React hook-like API.
View iterator.js
// Example usage:
// void async function() {
// let [clicks, onclick] = iterator()
// document.querySelector('button').addEventListener('click', onclick)
// for await (let click of clicks) console.log(click)
// }()
export default function iterator() {
let done = false
jed / s3-etag.js
Last active Mar 5, 2018
Create an etag for an S3 object. Assumes object is uploaded with exact chunk size.
View s3-etag.js
const {createReadStream} = require('fs')
const {createHash} = require('crypto')
module.exports = (path, chunkSize = 5242880) => {
return new Promise((resolve, reject) => {
const hashes = []
const options = {highWaterMark: chunkSize}
const rs = createReadStream(path, options)
rs.on('error', reject)
jed /
Last active Jul 15, 2022
Using AWS CloudFormation to deploy an edge lambda
aws cloudformation deploy \
--template-file stack.yaml \
--stack-name edge-lambda-test \
--capabilities CAPABILITY_IAM \
--parameter-overrides Nonce=$RANDOM
View template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Type: AWS::Serverless::Function
Handler: index.handler
Runtime: nodejs6.10
CodeUri: index.js
View jabberwocky.txt
'Twas brillig, and the slithy toves
Did gyre and gimble in the wabe:
All mimsy were the borogoves,
And the mome raths outgrabe.
"Beware the Jabberwock, my son!
The jaws that bite, the claws that catch!
Beware the Jubjub bird, and shun
The frumious Bandersnatch!"
So rested he by the Tumtum tree,
And stood awhile in thought.
jed / handler.js
Created May 16, 2016
Break Bot: pushing AWS IoT button events to Slack
View handler.js
'use strict'
// for context, see
const https = require('https')
const qs = require('querystring')
SINGLE : {emoji: ':coffee:', type: 'beverages'},