This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let sampleGenerator = function*() {}; | |
let noop = () => {}; | |
function *sleep(timer) { | |
return new Promise((res, rej) => { | |
setTimeout(() => { | |
res(); | |
}, timer); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$timers = []; | |
$futureTicks = []; | |
function setTimeout($callback, $delay) | |
{ | |
global $timers; | |
$delay = $delay === 0 ? 1 : $delay; | |
$timers[] = [ |