From 608c87628d339524e27e146941d935daca3b799e Mon Sep 17 00:00:00 2001 From: Freewind Date: Thu, 17 May 2012 14:35:56 +0800 Subject: [PATCH] demo for apply.js --- apply.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 apply.js diff --git a/apply.js b/apply.js new file mode 100644 index 0000000..e1d2f55 --- /dev/null +++ b/apply.js @@ -0,0 +1,25 @@ +var async = require('async'); + +var t = require('./t'); +var log = t.log; + +/** + * async.apply是一个非常好用的函数,可以让我们给一个函数预绑定多个参数并生成一个可直接调用的新函数,简化代码。 + * + * function(callback) { t.inc(3, callback); } + * 等价于: + * async.apply(t.inc, 3); + */ +async.parallel([ + async.apply(t.inc, 3), + async.apply(t.fire, 100) +], function (err, results) { + log('err: ', err); + log('results: ', results); +}); + +// 预设参数 +var x = async.apply(t.inc, 1); +x(function(err, n){ + console.log('1.inc: ' + n); +});