Skip to content

Instantly share code, notes, and snippets.

View korzio's full-sized avatar

Alex Korzhikov korzio

View GitHub Profile
┌───────────────────────────┐
┌─>│ timers │ # setTimeout, setInterval
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ pending callbacks │
│ └─────────────┬─────────────┘
│ ┌─────────────┴─────────────┐
│ │ idle, prepare │ # system
│ └─────────────┬─────────────┘ ┌───────────────┐
│ ┌─────────────┴─────────────┐ │ incoming: │
setTimeout(function first() {
console.log(1)
}, 1000)
console.log(2)
setTimeout(function second() {
console.log(3)
}, 2000)
document.addEventListener('click', () => {
console.log('click')
})
const http = require('http')
const hostname = '127.0.0.1'
const port = 3000
const server = http.createServer((req, res) => {
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.end('Hello World\n')
})
server.listen(port, hostname, () => {
const fs = require('fs')
fs.readFile('./events.js', function callback(err, data) {
console.log('file has been read')
})
{
"compilerOptions": {
"resolveJsonModule": true,
/* Basic Options */
// "incremental": true, /* Enable incremental compilation */
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
// "lib": [], /* Specify library files to be included in the compilation. */
// "allowJs": true, /* Allow javascript files to be compiled. */
// "checkJs": true, /* Report errors in .js files. */
function getResponseSize(url) {
return fetch(url).then(response => {
const reader = response.body.getReader()
let total = 0
return reader.read().then(function processResult(result) {
if (result.done) return total
const value = result.value
total += value.length
console.log('Received chunk', value)
return reader.read().then(processResult)
async function getResponseSizeAsync(url) {
const response = await fetch(url)
const reader = response.body.getReader()
let total = 0
while (true) {
const result = await reader.read()
if (result.done) {
return total
}
const { IncomingWebhook } = require('@slack/webhook')
const url = process.env.SLACK_WEBHOOK_URL
const webhook = new IncomingWebhook(url)
// Send the notification
(async () => {
await webhook.send({
text: 'I\'ve got news for you...',
})
})()
#!/usr/bin/env node
const { description, name, version } = require('./package.json')
const options = process.argv.slice(2)
const VERSION_MESSAGE = `${name} ${version}`
const HELP_MESSAGE = `${VERSION_MESSAGE}
${description}
Usage:
--help Help documentation