next generation web framework for node.js
const Koa = require('koa');
const app = new Koa();
// x-response-time
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
ctx.set('X-Response-Time', `${ms}ms`);
});
// logger
app.use(async (ctx, next) => {
const start = Date.now();
await next();
const ms = Date.now() - start;
console.log(`${ctx.method} ${ctx.url} - ${ms}`);
});
// response
app.use(async ctx => {
ctx.body = 'Hello World';
});
app.listen(3000);
// 耗費時間M
getData('from_db');
// 耗費時間N
getData('from_remote_api', function (result) {});
fs.readFile('file1.txt', 'utf8', function (err, txt) {
if (err) {
throw err;
}
fs.readFile(txt, 'utf8', function (err, content) {
if (err) {
throw err;
}
console.log(content);
});
});
fs.readFile('file1.txt', 'utf8', function (err, txt) {
if (err) {
throw err;
}
console.log(txt);
});
fs.readFile('file2.txt', 'utf8', function (err, content) {
if (err) {
throw err;
}
console.log(content);
});
fs.readdir(path.join(__dirname, './index.js'), (err, files) => {
files.foreach((filename, index) => {
fs.readFile((filename, 'utf-8', (err, file) => {
...
}))
})
})
var pending = (function () {
var count = 0;
return function (callback) {
count++;
return function () {
count--;
if (count === 0) {
callback();
}
};
};
}());
var done = pending(function () {
console.log('all is over');
});
fs.readFile('file1.txt', 'utf8', done());
fs.readFile('file2.txt', 'utf8', done());
https://repl.it/repls/JampackedDarkmagentaClimate
var x = 1;
function f(m) {
return m * 2;
}
f(x + 5);
// 傳值調用
// 先計算
f(x+5)
f(6)
// 傳名應用
// 只在用到時才計算
f(x + 5)
f(x + 5 * 2)
// 哪一個好?
// 傳值應用比較簡單,但以下範例就會做不必要求值
function f(a, b){
return b;
}
f(3 * x * x - 2 * x - 1, x);
function f(m){
return m * 2;
}
f(x + 5);
// 等同于
var thunk = function () {
return x + 5;
};
function f(thunk){
return thunk() * 2;
}
// 正常版本的readFile(多參數本)
fs.readFile(fileName, callback);
// Thunk版本的readFile(單参数版本)
var readFileThunk = Thunk(fileName);
readFileThunk(callback);
var Thunk = function (fileName){
return function (callback){
return fs.readFile(fileName, callback);
};
};
async 函数就是將 Generator 函数的星號(*)替换成 async,将 yield 替换成 await,僅此而已
就字面上意思也更好理解
一行入魂,ㄜ不是,一張 Gif 解釋洋蔥蔥