function Thenjs(start, debug) {
var self = this, cont;
if (start instanceof Thenjs) return start;
if (!(self instanceof Thenjs)) return new Thenjs(start, debug);
self._success = self._each = self._eachSeries = self._parallel = self._series = null;
self._finally = self._error = self._fail = self._result = self._nextThen = self._chain = null;
if (!arguments.length) return self;
cont = genContinuation(self, debug);
try {
if (typeof start === 'function') {
start(cont);
} else if (start == null) {
cont();
} else if (typeof start.toThunk === 'function') {
start.toThunk()(cont);
} else if (typeof start.then === 'function') {
start.then(function(res) {
cont(null, res);
}, cont);
} else {
cont(null, start);
}
} catch (error) {
cont(error);
}
}
Thenjs.defer = defer;
Thenjs.each = function (array, iterator, debug) {
return thenFactory(function (cont) {
defer(cont, each, cont, array, iterator);
}, null, debug);
};
Thenjs.eachSeries = function (array, iterator, debug) {
return thenFactory(function (cont) {
defer(cont, eachSeries, cont, array, iterator);
}, null, debug);
};
Thenjs.parallel = function (array, debug) {
return thenFactory(function (cont) {
defer(cont, parallel, cont, array);
}, null, debug);
};
Thenjs.series = function (array, debug) {
return thenFactory(function (cont) {
defer(cont, series, cont, array);
}, null, debug);
};
Thenjs.nextTick = function (fn) {
var args = slice(arguments, 1);
nextTick(function () {
fn.apply(null, args);
});
};