Skip to content

Instantly share code, notes, and snippets.

Avatar

Deepal Jayasekara deepal

View GitHub Profile
View chrome_timers.cpp
DOMTimer::DOMTimer(ExecutionContext* context, PassOwnPtrWillBeRawPtr<ScheduledAction> action, int interval, bool singleShot, int timeoutID)
: SuspendableTimer(context)
, m_timeoutID(timeoutID)
, m_nestingLevel(context->timers()->timerNestingLevel() + 1)
, m_action(action)
{
// ... redacted ...
double intervalMilliseconds = std::max(oneMillisecond, interval * oneMillisecond);
if (intervalMilliseconds < minimumInterval && m_nestingLevel >= maxTimerNestingLevel)
intervalMilliseconds = minimumInterval;
View timers.js
function Timeout(callback, after, args, isRepeat, isRefed) {
after *= 1; // Coalesce to number or NaN
if (!(after >= 1 && after <= TIMEOUT_MAX)) {
if (after > TIMEOUT_MAX) {
process.emitWarning(`${after} does not fit into` +
' a 32-bit signed integer.' +
'\nTimeout duration was set to 1.',
'TimeoutOverflowWarning');
}
after = 1; // Schedule on next tick, follows browser behavior
View nested-settimeout.js
const startHrTime = () => {
if (typeof window !== 'undefined') return performance.now();
return process.hrtime();
}
const getHrTimeDiff = (start) => {
if (typeof window !== 'undefined') return performance.now() - start;
const [ts, tns] = (process.hrtime(start));
return ts * 1e3 + tns / 1e6;
}
View microtasks-macrotasks.js
Promise.resolve().then(() => console.log('promise1 resolved'));
Promise.resolve().then(() => console.log('promise2 resolved'));
setTimeout(() => {
console.log('set timeout3')
Promise.resolve().then(() => console.log('inner promise3 resolved'));
}, 0);
setTimeout(() => console.log('set timeout1'), 0);
setTimeout(() => console.log('set timeout2'), 0);
Promise.resolve().then(() => console.log('promise4 resolved'));
Promise.resolve().then(() => {
View emitter.js
const EventEmitter = require('events');
const myEmitter = new EventEmitter();
myEmitter.on('myevent', () => console.log('handler1: myevent was fired!'));
myEmitter.on('myevent', () => console.log('handler2: myevent was fired!'));
myEmitter.on('myevent', () => console.log('handler3: myevent was fired!'));
myEmitter.emit('myevent');
console.log('I am the last log line');
View complete-async.js
function writeToMyFile(data, callback) {
if (!data) {
process.nextTick(() => callback(new Error('No data provided')));
} else {
fs.writeFile('myfile.txt', data, callback);
}
}
View sync-async.js
function writeToMyFile(data, callback) {
if (!data) {
callback(new Error('No data provided'));
} else {
fs.writeFile('myfile.txt', data, callback);
}
}
View cps.js
const sum = (a, b, callback) => {
callback(a + b);
};
sum(1,2, (result) => {
console.log(result);
});
View iife-async-1-noop.js
const wait = () => new Promise((resolve) => {
setTimeout(() => {}, 5000)
})(async () => {
return wait();
})()
View iife-async-2.js
const wait = () => new Promise((resolve) => {
setTimeout(() => {}, 5000)
})
const fn = (async () => {
return wait();
})()
You can’t perform that action at this time.