Standardize API promise patterns

When adding promise support to API methods, implement a consistent pattern that handles both callback-style and multi-parameter methods. Use a standardized promisification approach that appends the promise-determining callback as the last argument.

copy reviewer prompt

Prompt

Reviewer Prompt

When adding promise support to API methods, implement a consistent pattern that handles both callback-style and multi-parameter methods. Use a standardized promisification approach that appends the promise-determining callback as the last argument.

Example implementation:

function promisifyMethod(methodName, PromiseDependency) {
    return function promise() {
        var self = this;
        var args = Array.prototype.slice.call(arguments);
        return new PromiseDependency(function(resolve, reject) {
            args.push(function(err, data) {
                if (err) {
                    reject(err);
                } else {
                    resolve(data);
                }
            });
            self[methodName].apply(self, args);
        });
    };
}

This pattern ensures:

  • Consistent promise behavior across all API methods
  • Support for both simple callback methods and multi-parameter methods
  • Proper error handling and promise rejection
  • Compatibility with different promise implementations

Source discussions