Skip to content

Instantly share code, notes, and snippets.

View jfet97's full-sized avatar
🖤
Loving programming

Andrea Simone Costa jfet97

🖤
Loving programming
View GitHub Profile
@jfet97
jfet97 / ex1.js
Created December 7, 2018 12:34
ex1
(async function IIAFE(){
for await(const chunk of readableStream) {
// do stuff with each chunk
}
})();
@jfet97
jfet97 / asyncGeneratorsFactory.js
Last active December 7, 2018 13:20
Goodbye Transform-Streams, long live ES9 Async Generators
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;
@jfet97
jfet97 / ex3.js
Last active December 7, 2018 13:32
ex3.js
const { asyncGeneratorsFactory } = require('agh');
// filter function
const onlyOddPlease = function (n) {
if (n % 2) return n;
}
// transform function
const plusOne = function (n) {
return n + 1;
@jfet97
jfet97 / es4.js
Last active December 7, 2018 13:34
ex4
const { pipe } = require('agh');
(async function IIAFE() {
// pipe(inputStream, outputStream [, ...asyncGenerators]);
// it returns the outputStream
await pipe(misteriousStream, process.stdout, onlyOddPleaseGen, plusOneGen);
})();
@jfet97
jfet97 / ex2.js
Last active December 7, 2018 21:22
ex2
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;
@jfet97
jfet97 / 1_racePromisePool.js
Last active June 8, 2019 11:28
racePromisePool
const delay = ms => new Promise(ok => setTimeout(() => ok(ms), ms));
;(async () => {
const pool = [
delay(2000),
delay(1000),
delay(3000),
delay(500),
];
@jfet97
jfet97 / _objectsFlattening.js
Last active June 12, 2019 23:02
objects flattening
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,
@jfet97
jfet97 / asyncCancelablePolling.js
Last active June 13, 2019 14:50
Utility function to endlessy perform an async action until it returns a non null/undefined value. A max timeout could be setted as well as an interval to separate the consectutive calls to the async action, and the process is cancelable.
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;
@jfet97
jfet97 / index.js
Last active August 20, 2019 14:38
PromiseQueueAsyncGen
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 });
}
}
}
@jfet97
jfet97 / ReadableStreamAsyncIteratorPrototype.js
Last active September 13, 2019 12:52
ReadableStreamAsyncIteratorPrototype
const ReadableStreamAsyncIteratorPrototype = Object.create(AsyncIteratorPrototype, {
stream: {
get() {
return this[kStream];
},
enumerable: true,
configurable: true
},
next: {