lib/index.js
function koifyHandler(fn, args, isErrHandler) {
	let fnResult;

	if (isGeneratorFunction(fn)) {
		fnResult = co(function* () {
			return yield* fn(...args);
		});
	} else {
		fnResult = fn(...args);
	}

	if (!isPromise(fnResult)) {
		return fnResult;
	}

	let req, res, next;

	if (isErrHandler) {
		req = args[1];
		res = args[2];
		next = args[3];
	} else {
		req = args[0];
		res = args[1];
		next = args[2];
	}

	return fnResult.then((result) => {
		// implicit 'next'
		if ((result === req) || (result === res)) {
			next();
		// explicit 'next'
		} else if ((isFunction(next) && (result === next)) || (result === ko.NEXT)) {
			next();
		} else if (result === ko.NEXT_ROUTE) {
			next('route');
		} else if (isError(result)) {
			next(result);
		} else if (result !== undefined) {
			if (isNumber(result)) {
				res.sendStatus(result);
			} else {
				res.send(result);
			}
		}

		// exposed for testing
		return result;
	}, (err) => {
		next(err);
		return err;
	});
}
function koifyHandler(fn, args, isErrHandler) {
	let fnResult;

	if (isGeneratorFunction(fn)) {
		fnResult = co(function* () {
			return yield* fn(...args);
		});
	} else {
		fnResult = fn(...args);
	}

	if (!isPromise(fnResult)) {
		return fnResult;
	}

	let req, res, next;

	if (isErrHandler) {
		req = args[1];
		res = args[2];
		next = args[3];
	} else {
		req = args[0];
		res = args[1];
		next = args[2];
	}

	return fnResult.then((result) => {
		// implicit 'next'
		if ((result === req) || (result === res)) {
			next();
		// explicit 'next'
		} else if ((isFunction(next) && (result === next)) || (result === ko.NEXT)) {
			next();
		} else if (result === ko.NEXT_ROUTE) {
			next('route');
		} else if (isError(result)) {
			next(result);
		} else if (result !== undefined) {
			if (isNumber(result)) {
				res.sendStatus(result);
			} else {
				res.send(result);
			}
		}

		// exposed for testing
		return result;
	}, (err) => {
		next(err);
		return err;
	});
}
function koifyHandler ( fn , args , isErrHandler ) { let fnResult ; if ( isGeneratorFunction ( fn ) ) { fnResult = co ( function * ( ) { return yield * fn ( ... args ) ; } ) ; } else { fnResult = fn ( ... args ) ; } if ( ! isPromise ( fnResult ) ) { return fnResult ; } let req , res , next ; if ( isErrHandler ) { req = args [ 1 ] ; res = args [ 2 ] ; next = args [ 3 ] ; } else { req = args [ 0 ] ; res = args [ 1 ] ; next = args [ 2 ] ; } return fnResult . then ( ( result ) => { // implicit 'next'
 if ( ( result === req ) || ( result === res ) ) { next ( ) ; // explicit 'next'
 } else if ( ( isFunction ( next ) && ( result === next ) ) || ( result === ko . NEXT ) ) { next ( ) ; } else if ( result === ko . NEXT_ROUTE ) { next ( 'route' ) ; } else if ( isError ( result ) ) { next ( result ) ; } else if ( result !== undefined ) { if ( isNumber ( result ) ) { res . sendStatus ( result ) ; } else { res . send ( result ) ; } } // exposed for testing
 return result ; } , ( err ) => { next ( err ) ; return err ; } ) ; }
Optionally co-ifies a generator, then chains a promise
Optionally co - ifies a generator then chains a promise