Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const fs = require('fs')
const axios = require('axios')
callback
fs.readFile('test.txt', 'utf-8', (err, str) => {
if (err) {
console.error('Something bad!' + err)
}
console.log(str)
})
const request = () => {
return fs.promises.readFile('test.txt', 'utf-8').then((url) => {
for (let i = 0; i < 10000; i++) {
console.log(i)
}
return axios.get(url)
})
}
request().then(response => {
console.log(response.data)
})
console.log('Hey')
// // multi thread
// // 1 4 6 9 10
// // 2 3 5 7 8
// // ....
// // 1----3---2---
function login() {
return new Promise((resolve, reject) => {
setTimeout(() => { resolve() }, Math.random() * 1500)
})
}
for (let i = 0; i < 3; i++) {
login().then(() => { console.log('logged in ' + i)})
}
async function foo() {
for (let i = 0; i < 3; i++) {
await login().then(() => { console.log('logged in ' + i)})
}
}
foo()
foo()
function* fibo() {
let a = 1
let b = 1
while (true) {
const sum = a + b
a = b
b = sum
yield sum
}
}
const gen = fibo()
console.log(gen.next().value)
console.log(gen.next().value)
console.log(gen.next().value)
console.log(gen.next().value)
console.log(gen.next().value)
const x = () => {
this of parent
}
function x() {
console.log(this)
}
const z = x.bind({}) // bound function
z()
var (without scope)
const | let
class A {
foo() {
console.log(this.x)
}
}
class B extends A {
constructor() {
super()
this.x = 1
}
foo() {
console.log(this.x)
}
}
const x = new B()
x.foo = function () {
console.log(this.x)
}
x.foo = () => { console.log(this.x) }
x.foo()
1. x.foo ?
2. x.constructor.protype.foo ? (x.constructor = B)
3. x.constructor.constructor.prototype.foo
obj: prototype & constructor
function A() {
// constructor
this.x = 1
}
A.prototype.foo = function () {
console.log(this.x)
}
const a = new A()
a.foo()
const { Server } = require('http')
const middleware = []
// app.use(bodyParser.JSON)
middleware.push((req, res, next) => {
req.json = JSON.parse.data()
next()
})
// a.js
const x = Symbol()
const an = {}
an[x] = '3'
an['x'] = 3
module.exports = an
// b.js
an.x = 4
const server = new Server(async (req, res) => {
middleware[1](req, res, () => {
middleware[2](req, res, () => {
middleware[3]
})
})
})
server.listen(3000)
// -- native --
1233
false
undefined
typeof null is object!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment