Skip to content

Instantly share code, notes, and snippets.

@eugeneware
Created December 5, 2017 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugeneware/5ebcd5ad0038609e165f9cd55b79edc8 to your computer and use it in GitHub Desktop.
Save eugeneware/5ebcd5ad0038609e165f9cd55b79edc8 to your computer and use it in GitHub Desktop.
const tape = require('tape');
const it = require('tape-promise').default(tape);
const { promisify } = require('util');
class MyTest {
constructor (x, y) {
this.x = x;
this.y = y;
}
times (multiplier, cb) {
return cb(null, this.x * multiplier);
}
product (cb) {
return cb(null, this.x * this.y);
}
}
const MyClosure = (x, y) => {
return {
times: (multiplier, cb) => {
return cb(null, x * multiplier);
},
product: (cb) => {
return cb(null, x * y);
}
}
}
const MyClosureClass = (x, y) => {
class MyClosureTest {
times (multiplier, cb) {
return cb(null, x * multiplier);
}
product (cb) {
return cb(null, x * y);
}
}
return new MyClosureTest();
}
it('should be able to call class methods', (t) => {
t.plan(4);
let x = new MyTest(5, 9);
x.times(3, (err, result) => {
t.error(err);
t.equals(result, 15);
});
x.product((err, result) => {
t.error(err);
t.equals(result, 45);
});
});
it('should be able to convert to promises through prototypes', async (t) => {
t.plan(2);
let result;
let x = new MyTest(5, 9);
MyTest.prototype.timesAsync = promisify(MyTest.prototype.times);
MyTest.prototype.productAsync = promisify(MyTest.prototype.product);
result = await x.timesAsync(3);
t.equals(result, 15);
result = await x.productAsync();
t.equals(result, 45);
});
it('should be able to convert to promises through methods', async (t) => {
t.plan(2);
let result;
let x = new MyTest(5, 9);
x.timesAsync = promisify(x.times);
x.productAsync = promisify(x.product);
result = await x.timesAsync(3);
t.equals(result, 15);
result = await x.productAsync();
t.equals(result, 45);
});
it('should be able to convert to promises through call', async (t) => {
t.plan(2);
let result;
let x = new MyTest(5, 9);
result = await promisify(x.times).call(x, 3);
t.equals(result, 15);
result = await promisify(x.product).call(x);
t.equals(result, 45);
});
it('should be able to convert to promises through bind', async (t) => {
t.plan(2);
let result;
let x = new MyTest(5, 9);
result = await promisify(x.times).bind(x)(3);
t.equals(result, 15);
result = await promisify(x.product).bind(x)();
t.equals(result, 45);
});
it('should be able to call closure methods', (t) => {
t.plan(4);
let x = MyClosure(5, 9);
x.times(3, (err, result) => {
t.error(err);
t.equals(result, 15);
});
x.product((err, result) => {
t.error(err);
t.equals(result, 45);
});
});
it('should be able to promisify closure methods', async (t) => {
t.plan(2);
let x = MyClosure(5, 9);
let result;
result = await promisify(x.times)(3);
t.equals(result, 15);
result = await promisify(x.product)();
t.equals(result, 45);
});
it('should be able to call hybrid closure methods', (t) => {
t.plan(8);
let x = MyClosureClass(5, 9);
x.times(3, (err, result) => {
t.error(err);
t.equals(result, 15);
});
x.product((err, result) => {
t.error(err);
t.equals(result, 45);
});
let y = MyClosureClass(5, 9);
y.times(3, (err, result) => {
t.error(err);
t.equals(result, 15);
});
y.product((err, result) => {
t.error(err);
t.equals(result, 45);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment