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 IIAFE(){ | |
for await(const chunk of readableStream) { | |
// do stuff with each chunk | |
} | |
})(); |
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 asyncGeneratorsFactory = function (cb) { | |
return async function* (asyncIterable) { | |
for await (const chunk of asyncIterable) { | |
const res = await cb(chunk); | |
if (typeof res != 'undefined') yield res; | |
} | |
} | |
} | |
exports.asyncGeneratorsFactory = asyncGeneratorsFactory; |
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 { asyncGeneratorsFactory } = require('agh'); | |
// filter function | |
const onlyOddPlease = function (n) { | |
if (n % 2) return n; | |
} | |
// transform function | |
const plusOne = function (n) { | |
return n + 1; |
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 { pipe } = require('agh'); | |
(async function IIAFE() { | |
// pipe(inputStream, outputStream [, ...asyncGenerators]); | |
// it returns the outputStream | |
await pipe(misteriousStream, process.stdout, onlyOddPleaseGen, plusOneGen); | |
})(); |
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 * onlyOddPlease(input) { | |
for await(const n of input) { | |
// assuming that each chunk is a number | |
if(n%2) yield n; | |
} | |
} | |
async function * plusOne(input) { | |
for await(const n of input) { | |
yield n+1; |
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 delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms)); | |
;(async () => { | |
const pool = [ | |
delay(2000), | |
delay(1000), | |
delay(3000), | |
delay(500), | |
]; |
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 obj = { a: { b: 1, c: 2, d: { e:4 , f:[1,2,3,4,5,6,7,8,9,0]} }, g:42 }; | |
const res = Object.fromEntries(flatProps(objectRecursiveEntries(obj))); | |
/* | |
{ | |
"a.b": 1, | |
"a.c": 2, | |
"a.d.e": 4, | |
"a.d.f.0": 1, | |
"a.d.f.1": 2, |
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 asyncCancelablePolling = function() { | |
const delay = (ms, val = ms) => new Promise(ok => setTimeout(ok, ms, val)); | |
return function(action = async () => false, timeout = 10000, interval = 1000) { | |
let doPolling = true; | |
return { | |
result: Promise.race([ | |
(async () => { | |
let res = null; |
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 aitGen() { | |
let i = 1; | |
return { | |
async next() { | |
if(i > 5) return Promise.resolve({ value: void 0, done: true }); | |
const res = await fetch(`https://jsonplaceholder.typicode.com/posts/${i++}`).then(x => x.json()); | |
return Promise.resolve({ value: res, done: false }); | |
} | |
} | |
} |
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 ReadableStreamAsyncIteratorPrototype = Object.create(AsyncIteratorPrototype, { | |
stream: { | |
get() { | |
return this[kStream]; | |
}, | |
enumerable: true, | |
configurable: true | |
}, | |
next: { |
OlderNewer