<commit_before>var when = require('when');

module.exports = exports = function why(opts) {
  if(arguments.length === 0) {
    return exports.resume.apply(this, arguments);
  }
  return exports.create(opts);
}

exports.done = function done() {
  if(!exports.runningState || !exports.runningState.wrappedPromise) {
    throw error.INVALID_STATE_FOR_DONE();
  }
  var wrapped = exports.runningState.wrappedPromise;
  delete exports.runningState.wrappedPromise;
  return wrapped;
}

exports.create = function create(factory) {
  var wrapper = function wrapper() {
    var iterator = factory.apply(this, arguments);
    return exports.run(iterator);
  }
  wrapper.toString = function() {
    return 'why(' + factory.toString() + ')';
  }
  return wrapper;
};

exports.resume = function resume(throws) {
  var state = exports.runningState;
  var wrap = when.defer(), old = state.wrappedPromise;
  if(!old) { state.wrappedPromise = wrap.promise; }
  else {
    var list = state.wrappedPromise.list || [state.wrappedPromise];
    list.push(wrap.promise);
    state.wrappedPromise = when.all(list);
    state.wrappedPromise.list = list;
  }
  return function(err, result) {
    if(throws === false) { result = err; err = null; }
    if(err) { return wrap.reject(err); }
    if(arguments.length === 2) { return wrap.resolve(result); }
    var args = Array.prototype.slice.call(arguments, 1);
    wrap.resolve(args);
  };
}

exports.run = function run(iterator) {
  var def      = when.defer()
    , state    = { wrappedPromise: undefined }
    , callback = next.bind(next, 'next')
    , errback  = next.bind(next, 'throw');

  next('next');
  return def.promise;
  function next(verb, value) {
    function verify() {
      if(state.wrappedPromise !== undefined) {
        throw error.UNUSED_CALLBACK();
      }
    }

    try {
      var promise, response, oldState = exports.runningState;
      verify();
      exports.runningState = state;
      try {
        response = iterator[verb](value);
        while (!response.done) {
          if(response.value === 'cb') response = iterator[verb](exports.resume(true));<commit_msg>fix yield value resolve<commit_after>var when = require('when');
var slice = Array.prototype.slice;

module.exports = exports = function why(opts) {
  if(arguments.length === 0) {
    return exports.resume.apply(this, arguments);
  }
  return exports.create(opts);
}

exports.done = function done() {
  if(!exports.runningState || !exports.runningState.wrappedPromise) {
    throw error.INVALID_STATE_FOR_DONE();
  }
  var wrapped = exports.runningState.wrappedPromise;
  delete exports.runningState.wrappedPromise;
  return wrapped;
}

exports.create = function create(factory) {
  var wrapper = function wrapper() {
    var iterator = factory.apply(this, arguments);
    return exports.run(iterator);
  }
  wrapper.toString = function() {
    return 'why(' + factory.toString() + ')';
  }
  return wrapper;
};

exports.resume = function resume(throws) {
  var state = exports.runningState;
  var wrap = when.defer(), old = state.wrappedPromise;
  if(!old) { state.wrappedPromise = wrap.promise; }
  else {
    var list = state.wrappedPromise.list || [state.wrappedPromise];
    list.push(wrap.promise);
    state.wrappedPromise = when.all(list);
    state.wrappedPromise.list = list;
  }
  return function(err, result) {
    if(throws === false && arguments.length === 1) return wrap.resolve(err);
    else if(throws === false) return wrap.resolve(slice.call(arguments));
    else if(err) return wrap.reject(err);
    else if(arguments.length < 3) return wrap.resolve(result);
    else wrap.resolve(slice.call(arguments, 1));
  };
}

exports.run = function run(iterator) {
  var def      = when.defer()
    , state    = { wrappedPromise: undefined }
    , callback = next.bind(next, 'next')
    , errback  = next.bind(next, 'throw');

  next('next');
  return def.promise;
  function next(verb, value) {
    function verify() {
      if(state.wrappedPromise !== undefined) {
        throw error.UNUSED_CALLBACK();
      }
    }

    try {
      var promise, response, oldState = exports.runningState;
      verify();
      exports.runningState = state;
      try {
        response = iterator[verb](value);
        while (!response.done) {
          if(response.value === 'cb') response = iterator[verb](exports.resume(true));
{'fraction_non_alphanumeric': 0.14216701173222912, 'fraction_numerical': 0.001610305958132045, 'mean_word_length': 3.432212028542304, 'pattern_counts': {'":': 0, '<': 4, '<?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': '3237190', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1314, 'n_words': 286}