<commit_before>var isPromise = require("./is");

var FULFILLED = 1;
var REJECTED = 2;

function signal(resolvers, state, input){
  process.nextTick(function(){
    for(var i = 0, l = resolvers.length; i < l; i++){
      resolvers[i]._resolve(state, input);
    }
  });
}

function enqueue(resolver, state, input){
  process.nextTick(function(){
    resolver._resolve(state, input);
  });
}

exports.Promise = Promise;
function Promise(then){
  this.then = then;
}

exports.Pendable = Pendable;
function Pendable(){
  var pending = [];
  var fulfilled = false;
  var fulfillmentValue, rejectionReason;

  this.fulfill = function(value){
    if(pending){
      fulfilled = true;
      fulfillmentValue = value;

      signal(pending, FULFILLED, value);
      pending = null;
    }
    return this;
  };
  
  this.reject = function(reason){
    if(pending){
      rejectionReason = reason;

      signal(pending, REJECTED, reason);
      pending = null;
    }
    return this;
  };

  this.then = function(onFulfilled, onRejected){
    var resolver = new Resolver(onFulfilled, onRejected);
    if(pending){
      pending.push(resolver);
    }else if(fulfilled){
      enqueue(resolver, FULFILLED, fulfillmentValue);
    }else{
      enqueue(resolver, REJECTED, rejectionReason);
    }
    return resolver.promise;
  };
}

Pendable.prototype = new Promise();

Pendable.prototype.promise = function(){
  return new Promise(this.then);
};

exports.Resolver = Resolver;
function Resolver(onFulfilled, onRejected){
  this[FULFILLED] = onFulfilled;
  this[REJECTED] = onRejected;

  this.pending = true;
  this.fulfilled = false;
  this.pendable = null;
  this.result = null;

  var resolver = this;
  this.promise = new Promise(function(onFulfilled, onRejected){
    return resolver.then(onFulfilled, onRejected);
  });
}

Resolver.prototype.fulfill = function(value){
  return this._resolve(FULFILLED, value);
};

Resolver.prototype.reject = function(reason){
  return this._resolve(REJECTED, reason);
};

Resolver.prototype.then = function(onFulfilled, onRejected){
  if(this.pending && !this.pendable){
    this.pendable = new Pendable();
  }
 
  if(this.pendable){
    return this.pendable.then(onFulfilled, onRejected);
  }

  var resolver = this;
  if(this.fulfilled && onFulfilled){
    resolver = new Resolver(onFulfilled);
    enqueue(resolver, FULFILLED, this.result);
  }else if(!this.fulfilled && onRejected){
    resolver = new Resolver(null, onRejected);
    enqueue(resolver, REJECTED, this.result);
  }

  return resolver.promise;
};

Resolver.prototype._resolve = function(state, input){
  var hasHandler = this[state];
  if(hasHandler){
    var isFulfilled;
    try{
      this.result = this[state](input);
      isFulfilled = true;
    }catch(error){
      this.result = error;
    }

    this.pending = false;
    this.fulfilled = isFulfilled;
  }else if(state === FULFILLED){
    this.pending = false;
    this.fulfilled = true;
    this.result = input;
  }else if(state === REJECTED){
    this.pending = false;
    this.result = input;
  }

  var returnedPromise;
  if(hasHandler && this.fulfilled && isPromise(this.result)){
    returnedPromise = this.result;
    if(!this.pendable){
      this.pendable = new Pendable();
    }
  }

  if(!this.pending && this.pendable){
    if(this.fulfilled){
      if(returnedPromise){
        try{<commit_msg>Remove references to handlers after resolving<commit_after>var isPromise = require("./is");

var FULFILLED = 1;
var REJECTED = 2;

function signal(resolvers, state, input){
  process.nextTick(function(){
    for(var i = 0, l = resolvers.length; i < l; i++){
      resolvers[i]._resolve(state, input);
    }
  });
}

function enqueue(resolver, state, input){
  process.nextTick(function(){
    resolver._resolve(state, input);
  });
}

exports.Promise = Promise;
function Promise(then){
  this.then = then;
}

exports.Pendable = Pendable;
function Pendable(){
  var pending = [];
  var fulfilled = false;
  var fulfillmentValue, rejectionReason;

  this.fulfill = function(value){
    if(pending){
      fulfilled = true;
      fulfillmentValue = value;

      signal(pending, FULFILLED, value);
      pending = null;
    }
    return this;
  };
  
  this.reject = function(reason){
    if(pending){
      rejectionReason = reason;

      signal(pending, REJECTED, reason);
      pending = null;
    }
    return this;
  };

  this.then = function(onFulfilled, onRejected){
    var resolver = new Resolver(onFulfilled, onRejected);
    if(pending){
      pending.push(resolver);
    }else if(fulfilled){
      enqueue(resolver, FULFILLED, fulfillmentValue);
    }else{
      enqueue(resolver, REJECTED, rejectionReason);
    }
    return resolver.promise;
  };
}

Pendable.prototype = new Promise();

Pendable.prototype.promise = function(){
  return new Promise(this.then);
};

exports.Resolver = Resolver;
function Resolver(onFulfilled, onRejected){
  this[FULFILLED] = onFulfilled;
  this[REJECTED] = onRejected;

  this.pending = true;
  this.fulfilled = false;
  this.pendable = null;
  this.result = null;

  var resolver = this;
  this.promise = new Promise(function(onFulfilled, onRejected){
    return resolver.then(onFulfilled, onRejected);
  });
}

Resolver.prototype.fulfill = function(value){
  return this._resolve(FULFILLED, value);
};

Resolver.prototype.reject = function(reason){
  return this._resolve(REJECTED, reason);
};

Resolver.prototype.then = function(onFulfilled, onRejected){
  if(this.pending && !this.pendable){
    this.pendable = new Pendable();
  }
 
  if(this.pendable){
    return this.pendable.then(onFulfilled, onRejected);
  }

  var resolver = this;
  if(this.fulfilled && onFulfilled){
    resolver = new Resolver(onFulfilled);
    enqueue(resolver, FULFILLED, this.result);
  }else if(!this.fulfilled && onRejected){
    resolver = new Resolver(null, onRejected);
    enqueue(resolver, REJECTED, this.result);
  }

  return resolver.promise;
};

Resolver.prototype._resolve = function(state, input){
  var hasHandler = this[state];
  if(hasHandler){
    var isFulfilled;
    try{
      this.result = this[state](input);
      isFulfilled = true;
    }catch(error){
      this.result = error;
    }

    this.pending = false;
    this.fulfilled = isFulfilled;
  }else if(state === FULFILLED){
    this.pending = false;
    this.fulfilled = true;
    this.result = input;
  }else if(state === REJECTED){
    this.pending = false;
    this.result = input;
  }

  this[FULFILLED] = this[REJECTED] = null;

  var returnedPromise;
  if(hasHandler && this.fulfilled && isPromise(this.result)){
    returnedPromise = this.result;
    if(!this.pendable){
      this.pendable = new Pendable();
    }
  }

  if(!this.pending && this.pendable){
    if(this.fulfilled){
      if(returnedPromise){
        try{
{'fraction_non_alphanumeric': 0.13342159458664313, 'fraction_numerical': 0.00088261253309797, 'mean_word_length': 3.5086206896551726, 'pattern_counts': {'":': 0, '<': 5, '<?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': '1635747', 'n_tokens_mistral': 2332, 'n_tokens_neox': 2283, 'n_words': 465}