UNPKG

1.13 kBJavaScriptView Raw
1'use strict';
2
3exports.sortById = function sortById(left, right) {
4 return left._id < right._id ? -1 : 1;
5};
6
7var chai = require('chai');
8chai.use(require("chai-as-promised"));
9
10exports.should = chai.should();
11var Promise = require('bluebird');
12exports.Promise = Promise;
13
14exports.fin = function (promise, cb) {
15 return promise.then(function (res) {
16 var promise2 = cb();
17 if (typeof promise2.then === 'function') {
18 return promise2.then(function () {
19 return res;
20 });
21 }
22 return res;
23 }, function (reason) {
24 var promise2 = cb();
25 if (typeof promise2.then === 'function') {
26 return promise2.then(function () {
27 throw reason;
28 });
29 }
30 throw reason;
31 });
32};
33
34exports.promisify = function (fun, context) {
35 return function() {
36 var args = [];
37 for (var i = 0; i < arguments.length; i++) {
38 args[i] = arguments[i];
39 }
40 return new Promise(function (resolve, reject) {
41 args.push(function (err, res) {
42 if (err) {
43 return reject(err);
44 }
45 return resolve(res);
46 });
47 fun.apply(context, args);
48 });
49 };
50};
\No newline at end of file