From 30e50915afc577890d689e774fe017495fe78cb7 Mon Sep 17 00:00:00 2001 From: Dave Hughes Date: Mon, 21 Sep 2020 21:25:33 -0700 Subject: [PATCH] Handle edge case with no callbackArgs This is an alternative approach to fixing #23. For chrome functions that pass no arguments to their callbacks, we should try to propagate the function's own return value. Honestly, I'm pretty confused about how it is even able to work. It makes sense, kind of, but I'm surprised that I can access the function return inside an arrow function defined as an inline function argument. In any case, I tested and confirmed that this works. Fixes #23 and closes #26. --- chrome-extension-async.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/chrome-extension-async.js b/chrome-extension-async.js index eaf71d4..5e7caae 100644 --- a/chrome-extension-async.js +++ b/chrome-extension-async.js @@ -31,7 +31,7 @@ return new Promise((resolve, reject) => { try { // Try to run the original function, with the trimmed args list - f(...safeArgs, (...cbArgs) => { + const ret = f(...safeArgs, (...cbArgs) => { // If a callback was passed at the end of the original arguments if (callback) { @@ -50,7 +50,7 @@ resolve(cbObj); } else if (!cbArgs || cbArgs.length === 0) - resolve(); + resolve(ret); // if there were no callback args, resolve with the function's own return value else if (cbArgs.length === 1) resolve(cbArgs[0]); else