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 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'); |
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 writeToMyFile(data, callback) { | |
if (!data) { | |
process.nextTick(() => callback(new Error('No data provided'))); | |
} else { | |
fs.writeFile('myfile.txt', data, callback); | |
} | |
} |
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 writeToMyFile(data, callback) { | |
if (!data) { | |
callback(new Error('No data provided')); | |
} else { | |
fs.writeFile('myfile.txt', data, callback); | |
} | |
} |
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 sum = (a, b, callback) => { | |
callback(a + b); | |
}; | |
sum(1,2, (result) => { | |
console.log(result); | |
}); |
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 wait = () => new Promise((resolve) => { | |
setTimeout(() => {}, 5000) | |
})(async () => { | |
return wait(); | |
})() |
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 wait = () => new Promise((resolve) => { | |
setTimeout(() => {}, 5000) | |
}) | |
const fn = (async () => { | |
return wait(); | |
})() |
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 wait = () => new Promise((resolve) => { | |
setTimeout(() => {}, 5000) | |
}) | |
(async () => { | |
return wait(); | |
})() |
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 publicWorker = require('worker_threads'); | |
// ...redacted... | |
port.on('message', (message) => { | |
if (message.type === 'loadScript') { | |
const { | |
cwdCounter, | |
filename, | |
doEval, |
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 kPublicPort = Symbol('kPublicPort'); | |
// ...redacted... | |
const { port1, port2 } = new MessageChannel(); | |
this[kPublicPort] = port1; | |
this[kPublicPort].on('message', (message) => this.emit('message', message)); | |
// ...redacted... | |
this[kPort].postMessage({ | |
type: 'loadScript', |
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 {Worker, isMainThread, parentPort, workerData} = require('worker_threads'); | |
if (isMainThread) { | |
const worker = new Worker(__filename, {workerData: {num: 5}}); | |
worker.once('message', (result) => { | |
console.log('square of 5 is :', result); | |
}) | |
} else { | |
parentPort.postMessage(workerData.num * workerData.num) | |
} |