<commit_before>var isGeneratorFunction = require('is-generator-function')

function isGenerator (val) {
  return val && typeof val.next === 'function' && typeof val.throw === 'function'
}

function isPromise (val) {
  return val && typeof val.then === 'function'
}

function isObservable (val) {
  return val && typeof val.subscribe === 'function'
}

/**
 * internal caco resolver
 *
 * @param {function*} genFn - generator function
 * @param {array} args - arguments in real array form
 * @returns {promise} if no callback provided
 */
function _caco (genFn, args) {
  var self = this
  var done = false
  var nextQ = []
  var callback

  // pass caco next to generator function
  if (typeof args[args.length - 1] === 'function') callback = args.pop()
  args.push(next)

  var iter = isGenerator(genFn) ? genFn : genFn.apply(self, args)

  // callback stepper
  function step (err, val) {
    if (!iter) {
      if (!done) {
        done = true
        callback.apply(self, arguments)
      }
    } else {
      // generator step
      try {
        var state = err ? iter.throw(err) : iter.next(val)
        if (state.done) iter = null

        // resolve yieldable
        var isYieldable = caco._yieldable(state.value, step)

        // next if generator returned non-yieldable
        if (!isYieldable && !iter) next(null, state.value)
      } catch (err) {
        // catch err, break iteration
        done = true
        callback.call(self, err)
      }
    }
  }

  // callback stepper with nextTick delay
  // nextQ to guarantee ordering
  function next () {
    var args = Array.prototype.slice.call(arguments)
    nextQ.push(args)
    process.nextTick(function () {
      while (nextQ.length) {
        step.apply(self, nextQ.shift())
      }
    })
  }

  if (callback) {
    step()
  } else {
    // return promise if no callback<commit_msg>use one step per nextTick<commit_after>var isGeneratorFunction = require('is-generator-function')

function isGenerator (val) {
  return val && typeof val.next === 'function' && typeof val.throw === 'function'
}

function isPromise (val) {
  return val && typeof val.then === 'function'
}

function isObservable (val) {
  return val && typeof val.subscribe === 'function'
}

/**
 * internal caco resolver
 *
 * @param {function*} genFn - generator function
 * @param {array} args - arguments in real array form
 * @returns {promise} if no callback provided
 */
function _caco (genFn, args) {
  var self = this
  var ticking = false
  var callback

  // pass caco next to generator function
  if (typeof args[args.length - 1] === 'function') callback = args.pop()
  args.push(next)

  var iter = isGenerator(genFn) ? genFn : genFn.apply(self, args)

  // callback stepper
  function step (err, val) {
    if (!iter) {
      callback.apply(self, arguments)
    } else {
      // generator step
      try {
        var state = err ? iter.throw(err) : iter.next(val)
        if (state.done) iter = null

        // resolve yieldable
        var isYieldable = caco._yieldable(state.value, step)

        // next if generator returned non-yieldable
        if (!isYieldable && !iter) next(null, state.value)
      } catch (err) {
        // catch err, break iteration
        callback.call(self, err)
      }
    }
  }

  // callback stepper with nextTick delay
  function next () {
    var args = Array.prototype.slice.call(arguments)
    if (!ticking) {
      ticking = true
      process.nextTick(function () {
        ticking = false
        step.apply(self, args)
      })
    } else {
      // if another next() called during pausing,
      // break iter and callback
      iter = null
    }
  }

  if (callback) {
    step()
  } else {
    // return promise if no callback
{'fraction_non_alphanumeric': 0.12339055793991416, 'fraction_numerical': 0.000536480686695279, 'mean_word_length': 2.4981238273921202, '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': '269538', 'n_tokens_mistral': 1137, 'n_tokens_neox': 1116, 'n_words': 343}