1

As background, I have an async function that does some logic, and eventually (if the browser supports it) returns a Proxy object. This is to support older legacy code that expected an object which can have many different available functions, depending on the name of the controller requested. The controller can have many different functions on it, it depends on the API functions that are available on the controller, so we just allow any function name. For example a controller can have a "controller.xyz()" or "controller.test()" or even "controller.then()".

let x = 0;
function getController() {
  return new Proxy({}, {get: () => () => `test${x}`});
}
async function getControllerAsync() {
  return getController();
}
function test(controller) {
  console.log(controller.x());
}
test(getController());
getControllerAsync().then(test);
// Expected: test0 and test1 should output to console
// Actual: only test0 is output, the getControllerAsync() promise never resolves

The problem, though (I think), is that the async function (or a Promise resolve function) thinks the Proxy object is a "thenable" object, or at least I think so. I don't understand, though, if that were the case why would the the Promise not resolve to a string instead of never resolving?

codefactor
  • 1,616
  • 2
  • 18
  • 41
  • 1
    At a glance, `.then` needs to invoke the callback passed to it when the promise resolves. Yours just returns a string, so it never resolving makes sense to me (your callback being passed to .then is essentially just thrown away as far as I can guess). – CollinD Nov 09 '21 at 03:46
  • 1
    I think your assumption is correct. And the reason for the promise not resolve because when `then` is called it will provided two arguments, `resolve` and `reject` and you never call it so it never fulfilled. I get this from [Thenables](https://javascript.info/promise-chaining) – User 28 Nov 09 '21 at 04:58

0 Answers0