javascript - Bluebird PromisifyAll without any Async suffix, i.e. replace the original functions possible? -


bluebird has promisifyall function "promisifies entire object going through object's properties , creating async equivalent of each function on object , prototype chain."

it creates functions suffix async.

is possible replace old functions entirely? replaced functions work original functions addition return promise, thought should safe replace old functions entirely.

var object = {}; object.fn = function(arg, cb) { cb(null,1) }; bluebird.promisifyall(object);  object.fn      // not want  object.fnasync // => should replace `object.fn` 

there's option specify custom suffix option unfortunately doesn't work empty string

bluebird.promisifyall(object, {suffix: ''});  rangeerror: suffix must valid identifier 

the problem if walks prototype , places *async functions - need brand new copies of every object in prototype chain fail since libraries return own objects.

that - if you're using mongoose , you're getting collection object - library not know return promisified version - you'd have own copy of promisified version library won't play nice it. in addition, library calls own functions , changing signature break lot of internal code.

of course, if need 1 level deep and don't care prototype and you're not concerned internal calls - can accomplish it:

object.getownpropertynames(object).foreach(function(key){     object[key] = promise.promisify(object[key]); }); 

it's important understand not usual case though. there other apporoaches (like making function return promise if omit callback) in general they're not reliable.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -