Skip to content
This repository has been archived by the owner on Jan 22, 2025. It is now read-only.

Custom promise chaining #808

Open
darkdragon-001 opened this issue Apr 17, 2017 · 0 comments
Open

Custom promise chaining #808

darkdragon-001 opened this issue Apr 17, 2017 · 0 comments

Comments

@darkdragon-001
Copy link

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] };
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant