Skip to content

Instantly share code, notes, and snippets.

View iampava's full-sized avatar
💭
I code and teach JavaScript.

Alexandru Pavaloi iampava

💭
I code and teach JavaScript.
View GitHub Profile
@iampava
iampava / interval-example.js
Last active January 24, 2017 17:43
Skydiving through the Promise Land - timeout
var hasHeLanded = false;
function takeOff() {
setTimeout(function () {
hasHeLanded = true;
console.log("I just landed! Woooho!");
}, 5000);
}
function callFriendAndTalkVeryExcitedlyAboutYourPrototype() {
@iampava
iampava / callback-example.js
Created January 22, 2017 20:19
Skydiving throught the Promise Land - callback
function takeOff(callback) {
setTimeout(function () {
console.log("I just landed! Woooho!");
callback();
}, 5000);
}
function callFriendAndTalkVeryExcitedlyAboutYourPrototype() {
console.log("Hey friend, you will not believe what my mind stumbled upon...");
}
@iampava
iampava / promise-example.js
Created January 22, 2017 20:21
Skydiving through the Promise Land - promise
function takeOff(resolve) {
setTimeout(function () {
console.log("I just landed! Woooho!");
resolve();
}, 5000);
}
function callFriendAndTalkVeryExcitedlyAboutYourPrototype() {
console.log("Hey friend, you will not believe what my mind stumbled upon...");
}
@iampava
iampava / blocking.js
Last active January 24, 2017 17:50
Skydiving through the Promise Land - sync
console.log("I'm blocking you, but just a little!");
while(true){
console.log("I'm blocking you FOREVER, haha!");
}
@iampava
iampava / promise_error-example.js
Created January 23, 2017 21:53
Skydiving through the Promise Land - promise error
function takeOff(resolve, reject) {
setTimeout(function () {
console.log("Whooopsy! Wheater is bad!");
reject();
}, 5000);
}
function callFriendAndTalkVeryExcitedlyAboutYourPrototype() {
console.log("Hey friend, you will not believe what my mind stumbled upon...");
}
class Bootstrap {
constructor() {
this.initObj = { value: false }; //This object keeps the state
//Server data retrieval
Observable.merge(firstObs, secondObs)
.take(2)
.subscribe(
@iampava
iampava / sw-precache-script.js
Created July 19, 2017 17:05
Angular 2 offline support with sw-precache library
const fs = require('fs');
const swPrecache = require('sw-precache');
function getFiles(path, regExpArray) {
//Returns an erray of fileNames that respect the RegExp
return new Promise((resolve, reject) => {
fs.readdir(path, (err, items) => {
resolve(items.filter(item => regExpArray.some(regExp => regExp.test(item))).map(item => `${path}/${item}`));
});
@iampava
iampava / piggy-bank.js
Last active June 8, 2021 02:34
Hacking Javascript |
function PiggyBank(){
let money = [];
return {
store: function(index, value){
money[index] = value;
},
push: function(value){
money.push(value);
}
}
@iampava
iampava / hack.js
Last active August 2, 2017 16:38
Hacking Javascript |
var money;
Array.prototype.push = function(value){
this[this.length] = value; //we keep the intended push functionality so that mum doesn't notice we hacked the PiggyBank
money = this;
}
sharedPiggyBank.push('$22');
sharedPiggyBank.push('$33'); // money = ['$22', '$33'];
@iampava
iampava / freaking-awesome-hack.js
Last active August 2, 2017 16:39
Hacking Javascript |
var money;
sharedPiggyBank.store('push', function(value) {
// YES, we can add properties to arrays!
this[this.length] = value;
money = this;
});
sharedPiggyBank.push('$22'); // resolves to the push method we added and not to Array.prototype.push