-
Notifications
You must be signed in to change notification settings - Fork 4
/
example.js
52 lines (41 loc) · 1021 Bytes
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
var thunkify = require('./');
var Cal = function (a, b) {
this.a = a;
this.b = b;
};
Cal.prototype.plus = function(callback) {
var self = this;
setTimeout(function () {
callback(null, self.a + self.b);
}, 5);
};
Cal.prototype.minus = function (callback) {
var self = this;
setTimeout(function () {
callback(null, self.a - self.b);
}, 5);
};
module.exports = Cal;
exports.create1 = function (a, b) {
return thunkify(new Cal(a, b));
};
// or
exports.create2 = function (a, b) {
var cal = new Cal(a, b);
cal.plus = thunkify(cal.plus, cal);
cal.minus = thunkify(cal.minus, cal);
};
var cal1 = exports.create1(1, 2);
cal1.plus()(function (err, res) {
console.log('cal1 plus result is ', res);
});
cal1.minus()(function (err, res) {
console.log('cal1 minus result is ', res);
});
var cal2 = exports.create1(1, 2);
cal1.plus()(function (err, res) {
console.log('cal2 plus result is ', res);
});
cal1.minus()(function (err, res) {
console.log('cal2 minus result is ', res);
});