Skip to content

Instantly share code, notes, and snippets.

View ZhihaoLau's full-sized avatar
💭
Meh

lzo ZhihaoLau

💭
Meh
View GitHub Profile
@ZhihaoLau
ZhihaoLau / unsettledPromise.js
Last active September 8, 2016 09:56
Creating Unsettled Promises
var fs = require('fs');
function readFile(filename) {
return new Promise(function(resolve, reject) {
fs.readFile(filename, {encoding: 'utf8'}, function(err, data) {
if (err) {
reject(err);
return;
}
resolve(data);
@ZhihaoLau
ZhihaoLau / settledPromise.js
Created September 8, 2016 11:48
Creating Settled Promise
let promise = Promise.resolve('hello');
promise.then(function(data) {
console.log(data);
});
// Or: ------------------------
let promise = Promise.reject('oops');
@ZhihaoLau
ZhihaoLau / createIterator.js
Last active October 5, 2016 16:14
createIterator - the es5 way
function createIterator(items) {
var i = 0;
return {
next: function() {
// NZakas' way is really straight forward what 'done' and 'value' is.
var done = (i >= items.length);
var value = done ? undefined : items[i++];
return {
done: done,
@ZhihaoLau
ZhihaoLau / createIterator.js
Created October 5, 2016 16:14
createIterator - generator way
function *createIterator(items) {
for(let i = 0; i < items.length; i++) {
yield items[i];
}
}
let it = createIterator([1, 2, 3]);
console.log(it.next()); // Object {value: 1, done: false}
console.log(it.next()); // Object {value: 2, done: false}
@ZhihaoLau
ZhihaoLau / shorthandGenerator.js
Created October 5, 2016 16:30
ES6 method shorthand generator
let o = {
*createIterator(items) {
for (let i = 0; i < items.length; i++) {
yield items[i];
}
}
};
let it = o.createIterator([1, 2, 3]);
@ZhihaoLau
ZhihaoLau / recursion.js
Created October 12, 2016 02:41
recursion.js
let categories = [
{ id: 'animals', 'parent': null },
{ id: 'mammals', 'parent': 'animals' },
{ id: 'cats', 'parent': 'mammals' },
{ id: 'dogs', 'parent': 'mammals' },
{ id: 'chihuahua', 'parent': 'dogs' },
{ id: 'persian', 'parent': 'cats' },
{ id: 'siamese', 'parent': 'cats' },
]
@ZhihaoLau
ZhihaoLau / times.js
Created October 14, 2016 02:42
‘Ruby times method’ in JavaScript
// Ruby = 5.times { |i| puts i }
// JS = (1).times(function(i){console.log(i);})
Number.prototype.times = function(cb) {
var i = -1;
while (++i < this) {
cb(i);
}
return +this;
@ZhihaoLau
ZhihaoLau / fakeMap.js
Created October 14, 2016 06:34
fakeMap in Array@reduce
var arr = [1, 2, 3, 4];
function handleNext(value) {
return value * 2
}
Array.prototype.fakeMap = function(cb) {
return this.reduce((accumulator, next, index) => {
@ZhihaoLau
ZhihaoLau / configurableLogger.js
Created October 15, 2016 16:11
configurable middleware example
function configableLogger(format) {
var re = /:(\w+)/g;
return function(req, res, next) {
var str = format.replace(re, (match, property) => {
return req[property];
});
console.log(str);
next();
}
}
@ZhihaoLau
ZhihaoLau / promise.js
Created February 21, 2017 03:24 — forked from jish/promise.js
An example "always" behavior for ES6 promises. This only works if you do not create / return intermediate promises.
// A thing I want to do
// This flow only involves **one** promise, for example an ajax call
// None of the subsequent `then` or `catch` calls, return new promises.
var explode = false;
var promise = new Promise(function(resolve, reject) {
if (explode) {