Skip to content

Instantly share code, notes, and snippets.

View lei-clearsky's full-sized avatar

Lei Zhu lei-clearsky

View GitHub Profile
// Problem 1
(() => {
console.log('this is the start');
setTimeout(function cb() {
console.log('this is a msg from call back');
});
console.log('this is just a message');
setTimeout(function cb1() {
console.log('this is a msg from call back1');
}, 0);
// JS Hoisting
function parent() {
var hoisted = "I'm a variable";
function hoisted() {
return "I'm a function";
}
return hoisted();
}
console.log(parent());
@lei-clearsky
lei-clearsky / simplePromise.js
Last active October 30, 2018 16:24
Simple Promise
// https://levelup.gitconnected.com/understand-javascript-promises-by-building-a-promise-from-scratch-84c0fd855720
// http://thecodebarbarian.com/write-your-own-node-js-promise-library-from-scratch.html
class PromiseSimple {
constructor(executionFunc) {
this.chains = [];
this.handleError = () => { console.log('err!'); };
this.onResolve = this.onResolve.bind(this);
this.onReject = this.onReject.bind(this);
executionFunc(this.onResolve, this.onReject);
/*
* Difficulty: Easy
* Create an event emitter that goes like this
* emitter = new Emitter();
*
* Allows you to subscribe to some event
* emitter.subscribe('event1', callback1);
* (you can have multiple callbacks to the same event)
* emitter.subscribe('event1', callback2);
*
class PromiseSimple {
}
fakeApiBackend = () => {
const user = {
username: 'John Doe',
favoriteNumber: 42,
profile: 'https://gitconnected.com/johndoe'
};
@lei-clearsky
lei-clearsky / README.md
Last active August 29, 2015 14:28 — forked from hofmannsven/README.md
My simply Git Cheatsheet
function GameOfLife(width,height) {
this.width = width;
this.height = height;
this.nextState = [];
}
GameOfLife.prototype.createAndShowBoard = function () {
// create <table> element
var goltable = document.createElement("tbody");
var traverseDomAndCollectElements = function(matchFunc, startEl) {
var resultSet = [];
if (typeof startEl === "undefined") {
startEl = document.body;
}
// if (matchFunc(startEl))
// resultSet.push(startEl);
def get_from_digg
res = JSON.load(RestClient.get('http://digg.com/api/news/popular.json'))
res["data"]["feed"].map do |story|
s = {}
tags = []
s[:title] = story["content"]["title"]
story["content"]["tags"].map do |tag|
tags << tag["display"]
end
s[:category] = tags.join(", ")