聲明:部分內容結合其他帖子,僅做記錄,如侵刪.
generator生成器函數
function* test() { console.log('1'); yield 1; console.log('2'); yield 2; console.log('3'); }
yield
yield相當于打斷點
進階知識:generator的Delegating yield - 代理斷點 (待處理)
thunk函數[疑惑]
thunk函數是一個偏函數,執行它會得到一個新的只帶一個回調參數的函數
var fs = require('fs'); function size(file) { //這里返回一個可以傳遞fn的函數 return function(fn){ //這個函數中使用傳遞進來的fn fs.stat(file, function(err, stat){ if (err) return fn(err); else{ // console.log(stat.size); fn(null, stat.size); } }); } } var getIndexSize = size("./index.js"); getIndexSize(function(size,infactSize){ // console.log(size); console.log(infactSize) })
thunk函數就是返回一個新的回調函數
- 最簡單的co實現
實驗:
原理
Bash中可以使用echo來進行輸出。
$ echo Something
$ echo -e \tSomething\n