<commit_before>(function() {
  'use strict';
  
  define([], function() {
    
    return {
      makePromise: makePromise
    };
    
    function makePromise() {
      
    }
  });
})();<commit_msg>first implementation chainable promise with error handling<commit_after>(function() {
  'use strict';
  
  define([], function() {
    
    return {
      promise: makePromise
    };
    
    function makePromise() {
      var _state = 'pending';
      var _value = undefined;
      var _dependencies = []; // [{onResolved,onRejected}]

      return {
        then: then,
        resolve: resolve,
        reject: reject
      }

      function resolve(value) {
        if (_state !== 'pending') {
          throw new Error('already resolved/rejected!');
        }
        _value = value;
        var dependencies = _dependencies;
        dependencies.forEach(function(continuation) {
          continuation.onResolved(value);
        });
        _dependencies = [];
        _state = 'resolved';
      }

      function reject(error) {
        if (_state !== 'pending') {
          throw new Error('already resolved/rejected!');
        }
        _value = error;
        var dependencies = _dependencies;
        dependencies.forEach(function(continuation) {
          continuation.onRejected(error);
        });
        _dependencies = [];
        _state = 'rejected';
      }

      function then(onResolved, onRejected) {
        if (_state === 'resolved') {
          return onResolved(_value);
        }
        if (_state === 'rejected') {
          return onRejected(_value);
        }
        var result = makePromise();
        _dependencies.push({
          onResolved: onResolved(_value).then(succeed, fail),
          onReject: onRejected(_value).then(succeed, fail)
        });
        return result;

        function succeed(value) {
          result.resolve(value);
          return makePromise();
        }

        function fail(error) {
          result.reject(error);
          return makePromise();
        }
      }
    }
  });
})();
{'fraction_non_alphanumeric': 0.12408759124087591, 'fraction_numerical': 0.0, 'mean_word_length': 1.8675034867503486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3181660', 'n_tokens_mistral': 568, 'n_tokens_neox': 547, 'n_words': 115}