Back to all reviewers

Standardize API promise patterns

aws/aws-sdk-js
Based on 2 comments
JavaScript

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.

API JavaScript

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
2
Comments Analyzed
JavaScript
Primary Language
API
Category

Source Discussions