Skip to content

Instantly share code, notes, and snippets.

View myndzi's full-sized avatar

Kris Reeves myndzi

  • Sigma Bold
  • Olympia, WA
View GitHub Profile
function sequentially(array, promiseFn) {
return array.reduce((acc, current) =>
acc.then(results =>
promiseFn(current).then(res =>
results.concat(res)
)
)
, Promise.resolve([]);
}
const Transform = require('stream').Transform;
const util = require('util');
function ChunkStream(options) {
if (!(this instanceof ChunkStream))
return new ChunkStream(options);
Transform.call(this, options);
this.chunkSize = options.chunkSize || 1500;
this._chunks = [ ];
// paginate('get', '/foo', {...})
const paginate = (method, path, opts, offset, results) => {
offset = offset || 0;
results = results || [ ];
return Promise.try(() =>
request(method, path, Object.assign(opts, { offset }))
).then(res => {
let newResults = results.concat(res.data);
if (!res.pagination) { return newResults; }
@myndzi
myndzi / .js
Created December 23, 2016 03:00
function getFoos(nextOffset, results) {
nextOffset = nextOffset || 0;
results = results || [ ];
return Promise.try(() => {
return getSomeFoos(nextOffset);
}).then(res => {
let newResults = results.concat(res.foos);
return res.hasMore ?
getFoos(res.nextOffset, newResults) :
@myndzi
myndzi / .js
Created December 11, 2016 22:34
var qb = knex.select('songs.id', 'songs.name AS song', 'artists.name AS artist', 'songs.path', 'albums.name AS album')
.from('songs')
.join('genreAssociation', 'songs.id', 'genreAssociation.id_songs')
.join('artists', 'songs.id_artists', 'artists.id')
.join('albums', 'songs.id_albums', 'albums.id')
.where('genreAssociation.id', req.params.id)
if (req.session.playedSongs && req.session.playedSongs.length != 0 && req.session.playedSongs != []) {
qb.whereNotIn('songs.id', req.session.playedSongs);
}
foo bar
1 2
3 4
5 6
7 8
@myndzi
myndzi / .js
Last active November 3, 2016 06:06
var split = require('binary-split-streams2'),
fs = require('fs');
function requestStream(url) {
return Kefir.stream(emitter => {
let req = bhttp.get(url, { stream: true })
.pipe(split()); // default is \n -- no regex support, use a different module if you need variable line terminators
req.on('data', chunk => emitter.emit(chunk));
req.on('error', err => {
emitter.error(err); emitter.end();
@myndzi
myndzi / .js
Last active August 10, 2016 02:59
var EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits;
var ID = 0;
function Fetcher(url) {
EventEmitter.call(this);
this.id = ID++;
this.url = url;
}
myndzi@seckzi:~/test$ node foo
pg pool: function (options) {
var config = { Client: Client };
for (var key in options) {
config[key] = options[key];
}
Pool.call(this, config);
}
myndzi@seckzi:~/test$ npm ls
[email protected] /home/myndzi/test
@myndzi
myndzi / .js
Last active August 1, 2016 00:54
function delay(ms) {
// error checking wouldn't hurt
return new Promise(resolve => setTimeout(resolve, delay));
}
function retryWithDelay(fn, delay, retries) {
if (retries <= 0) {
return Promise.reject(new Error('out of retries'));
}
return fn().catch(err => {
// typed catch or checking the kind of error here would be a good thing