Permalink
Browse files

feat: let toshihiko support promise

  • Loading branch information...
XadillaX committed Oct 19, 2016
1 parent 999454e commit 333ff8175c4db35963242b50d3e69e408906f7d7
Showing with 30 additions and 0 deletions.
  1. +30 −0 lib/toshihiko.js
@@ -11,6 +11,8 @@ const EventEmitter = require("eventemitter2").EventEmitter2;

const common = require("../util/common");

const emptyFunc = function() {};

class Toshihiko extends EventEmitter {
constructor(Adapter, options) {
super();
@@ -34,7 +36,35 @@ class Toshihiko extends EventEmitter {
}

execute() {
const promise = common.promisify();

let trueCallback = emptyFunc;
let cbIdx;
for(let i = 0; i < arguments.length; i++) {
if(typeof arguments[i] === "function") {
trueCallback = arguments[i];
cbIdx = i;
}
}
let callback = function(err, res, extra) {
if(err) {
promise.reject(err);
} else {
promise.resolve(res);
}

trueCallback(err, res, extra);
};
if(cbIdx === undefined){
arguments[arguments.length] = callback;
arguments.length++;
} else {
arguments[cbIdx] = callback;
}

this.adapter.execute.apply(this.adapter, arguments);

return promise.q;
}

define(collectionName, schema, options) {

0 comments on commit 333ff81

Please sign in to comment.