This repository was archived by the owner on Jan 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
This repository was archived by the owner on Jan 22, 2025. It is now read-only.
Custom promise chaining #808
Copy link
Copy link
Open
Description
Description
I would like to extend Q's functionality a bit. I would like to add custom chaining functions (Q().CUSTOM()
).
I noticed that I probably had to overwrite the fulfill
method copying the existing object passed to the constructor of Promise
. I would prefer to be able to add to this implicitely declared object.
Examples
I would like to add the following functions for my own projects. Since there seems to be no interest in adding it to the default functionality (#780), I would like to import Q and afterwards add some extensions in my scripts.
Q().map(func) [synchronous]
Q().then(function(result){
return Q.all(result.map(func));
});
Q().map(func) [asynchronous]
Q().then(function(result){
var deferred = Q.defer();
return async.map(result, func, function(err, res){
if (err) {
return deferred.reject(new Error(err));
} else {
return deferred.resolve(res);
}
);
});
Q().tee()
Q().then(function(result){
console.log(result);
return result;
});
Desired API
I would like to have an API similar to
Q.addMethods({
'mapS': function(result){
return Q.all(result.map(func));
},
'mapA': Q.denodeify(async.map),
'tee': function(result){
console.log(result);
return result;
}
});
which basically adds them to the object passed to the Promise
constructor on fulfillment and adds the following methods:
Q.CUSTOM = (object, ...args) => Q(object).dispatch(CUSTOM, [...args]);
Promise.prototype.CUSTOM = function(...args) { this.dispatch(CUSTOM, [...args] };
Metadata
Metadata
Assignees
Labels
No labels