Skip to content

Commit

Permalink
Added loose Promise detection
Browse files Browse the repository at this point in the history
  • Loading branch information
nuxy committed Mar 28, 2023
1 parent 257247d commit 11e8a90
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/router/Stack.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class RouterStack {
lastItem = index++ === funcs.length;
nextItem = false;

if (isAsyncFunc(func) || isPromise(func)) {
if (isAsyncFunc(func) || isPromise(func) || hasReturn(func)) {

// Asynchronous handling.
promises.push(() => {
Expand All @@ -146,4 +146,18 @@ class RouterStack {
}
};

/**
* Check if return statement exists. Assumes Promise
* since there is no way to detect if a function
* returns a Promise event without execution.
*
* @param {Function} func
* Route/Middleware Function.
*
* @return {Boolean}
*/
function hasReturn(func) {
return (func && (typeof func === 'function' && /{\s+return\s+/.test(func.toString())));
}

module.exports = RouterStack;

0 comments on commit 11e8a90

Please sign in to comment.