. Q , , Q ( OP), . Bergi. lib, , ; , . .
, Node - promises ( ), :
count: function() {
return makePromise(User.count)
}
makePromise
:
function makePromise(f) {
var deferred = Q.defer()
var args = Array.prototype.slice.call(arguments)
args[0] = function(err) {
if(err)
deferred.reject(err)
else
deferred.resolve(Array.prototype.slice.call(arguments, 1))
}
f.apply(null, args)
return deferred.promise
}
( ;
, ASI )
, , , , err
, , err
. Node, err
.
, - : promisify
:-) , , .
thisArg
:
function makePromise(obj, f) {
var deferred = Q.defer()
var args, func, thisArg;
if (typeof obj === "function") {
thisArg = null
func = obj
args = Array.prototype.slice.call(arguments)
} else {
thisArg = obj
func = f
args = Array.prototype.slice.call(arguments, 1)
}
args[0] = function(err) {
if(err)
deferred.reject(err)
else
deferred.resolve(Array.prototype.slice.call(arguments, 1))
}
func.apply(thisArg, args)
return deferred.promise
}
, User.count
this
= User
:
count: function() {
return makePromise(User, User.count)
}