Skip to content

Commit

Permalink
feat: let toshihiko support promise
Browse files Browse the repository at this point in the history
  • Loading branch information
XadillaX committed Oct 19, 2016
1 parent 999454e commit 333ff81
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/toshihiko.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ const EventEmitter = require("eventemitter2").EventEmitter2;


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


const emptyFunc = function() {};

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


execute() { 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); this.adapter.execute.apply(this.adapter, arguments);

return promise.q;
} }


define(collectionName, schema, options) { define(collectionName, schema, options) {
Expand Down

0 comments on commit 333ff81

Please sign in to comment.