skewer

0.0.9 • Public • Published

Skewer

node-ttucsc 大同大學資訊創意研究社 node.js 第二堂課課程:npm + flow control - 自幹簡易 flow control module

  • npm 安裝/移除 module
  • package.josn
  • npm 發布 module
  • 常見 flow control 方法
  • 自幹簡易 flow control module

範例安裝

npm install skewer

用法

skewer()

正宗串燒阿...

用幾個 object 串起數個 function

skewer(function (err, obj) {
  console.log(obj);
})
  .add(function (obj, next) {
    obj.idiot = '馬囧';
    next();
  })
  .add(function (obj, next) {
    obj.idiot2 = '馬囧囧';
    next();
  })
  .run({});

skewer.chain()

鍊子!當前函式的參數由上一步的函式決定

skewer.chain(function (err, text) {
  console.log(b);
})
  .add(function (n, next) {
    next(null, 'xdd');
  })
  .add(function (a, b) {
    next(b);
  });

skewer.customs()

去你的海關,扣我 Geeksphone 這麼多的稅...

按照一開始函數傳入的變數名稱把結果排序並傳入函式

var emit = skewer.customs(function (error, text1, text2) {
  console.log(text1);
  console.log(text2);
});

setTimeout(function () {
  emit('text2', '馬囧');
}, 100);

setTimeout(function () {
  emit('text1', '123');
}, 200);

skewer.map()

把多個相似的異步處理寫在一起,最後把值按順序放入陣列

var square = {
  exponent: 2,
  calc: function (n, cb) {
    var val = Math.pow(n, this.exponent);
    setTimeout(function () {
      cb(null, val);
    }, 100);
  }
};
skewer.map([1, 2, 3], skewer.methodWrapper(square, square.calc), function (err, results) {
  results.toString().should.equal('1,4,9');
  done();
});

也可以傳入一些選項,像是 fs.readFile 可能會需要給 'utf8',如果有多個選項要設定,就按順序放在 'utf8' 後面

skewer.map(['./file1', './file2', './file3'], fs.readFile, 'utf8', function (err, results) {
  //...
});

skewer.filter()

把陣列值一個一個丟進 function 內執行,回傳結果再去跟設定的的值去作比對,最後只留下比對成功的值

選項設定方式跟 map 一樣

skewer.filter(['./file1', './file2', './file3'], fs.exists, true, function (results) {
  //...
});

skewer.thread()

skewer.thread(6, 1000, function (file, done) {
  //...
});

License

(The MIT License)

Copyright (c) 2013 Po-Ying Chen

Readme

Keywords

none

Package Sidebar

Install

npm i skewer

Weekly Downloads

1

Version

0.0.9

License

MIT

Last publish

Collaborators

  • poying