Skip to content

Commit

Permalink
Handle edge case with no callbackArgs
Browse files Browse the repository at this point in the history
This is an alternative approach to fixing KeithHenry#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 KeithHenry#23 and closes KeithHenry#26.
  • Loading branch information
dlh3 authored Sep 22, 2020
1 parent 39f4d72 commit 30e5091
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chrome-extension-async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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
Expand Down

0 comments on commit 30e5091

Please sign in to comment.