Skip to content

Instantly share code, notes, and snippets.

View sina42048's full-sized avatar
🏠
Working from home

Sina sina42048

🏠
Working from home
View GitHub Profile
<?php
$timers = [];
$futureTicks = [];
function setTimeout($callback, $delay)
{
global $timers;
$delay = $delay === 0 ? 1 : $delay;
$timers[] = [
@sina42048
sina42048 / asyncLoop.js
Last active October 21, 2021 08:14
behind the scene of async await loop
const delay = (timeout) => new Promise((res, rej) => setTimeout(res, timeout));
// (async function () {
// for (let i = 0; i < 10; i++) {
// console.log(i);
// await delay(2000);
// }
// })();
// is equivalent to
@sina42048
sina42048 / simple-react-hooks.js
Last active October 16, 2021 17:29
simple react hooks implementation to see how its work behind the scene
function fnName() {
try {
throw new Error();
} catch (e) {
// matches this function, the caller and the parent
const allMatches = e.stack.match(/(\w+)@|at (\w+) \(/g);
// match parent function name
const parentMatches = allMatches[1].match(/(\w+)@|at (\w+) \(/);
// return only name
return parentMatches[1] || parentMatches[2];
@sina42048
sina42048 / eventemitter.js
Created December 21, 2020 07:24
simple event emitter implementation in nodejs
const process = require('process');
function Event() {
this.events = [];
}
Event.prototype.emit = function(eventName) {
process.nextTick(() => {
const id = this.events.findIndex((event) => event.name === eventName);
for (observer of this.events[id].observers) {
@sina42048
sina42048 / generator-runner.js
Last active December 20, 2020 14:02
simple generator function runner with requestAnimationFrame in javascript !
let sampleGenerator = function*() {};
let noop = () => {};
function *sleep(timer) {
return new Promise((res, rej) => {
setTimeout(() => {
res();
}, timer);
});
}