|
| 1 | +"use strict"; |
| 2 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 3 | +/* tslint:disable:no-shadowed-variable */ |
| 4 | +const test = require("blue-tape"); |
| 5 | +const index_1 = require("./index"); |
| 6 | +const command = function (command, callback) { |
| 7 | + const promise = Promise.resolve().then(function () { |
| 8 | + return Promise.resolve('do some work and return result ' + command); |
| 9 | + }).then(function (data) { |
| 10 | + //console.log(data); |
| 11 | + return data; |
| 12 | + }); |
| 13 | + return index_1.default(promise, callback); |
| 14 | +}; |
| 15 | +const commandWillFail = function (command, callback) { |
| 16 | + const promise = Promise.resolve().then(function () { |
| 17 | + return Promise.reject('do some work and reject ' + command); |
| 18 | + }).then(function (data) { |
| 19 | + //console.log(data); |
| 20 | + return data; |
| 21 | + }); |
| 22 | + return index_1.default(promise, callback); |
| 23 | +}; |
| 24 | +test('nodeify', t => { |
| 25 | + t.test('promise', t => { |
| 26 | + return command('with promise').then(function (data) { |
| 27 | + t.equal(data, 'do some work and return result with promise'); |
| 28 | + }); |
| 29 | + }); |
| 30 | + t.test('callback', t => { |
| 31 | + command('with callback', function (err, data) { |
| 32 | + t.equal(data, 'do some work and return result with callback'); |
| 33 | + t.end(); |
| 34 | + }); |
| 35 | + }); |
| 36 | +}); |
| 37 | +test('nodeify should fail', t => { |
| 38 | + t.test('promise', t => { |
| 39 | + return commandWillFail('with promise').then(function (data) { |
| 40 | + console.log('data', data); |
| 41 | + }).catch((e) => { |
| 42 | + t.equal(e, 'do some work and reject with promise'); |
| 43 | + }); |
| 44 | + }); |
| 45 | + t.test('callback', t => { |
| 46 | + commandWillFail('with callback', function (err, data) { |
| 47 | + t.equal(err, 'do some work and reject with callback'); |
| 48 | + t.equal(null, data); |
| 49 | + t.end(); |
| 50 | + }); |
| 51 | + }); |
| 52 | +}); |
0 commit comments