1

I have the following promise code in a existing open source library:

simplified code is following:

class SignIn extends React.Component {
  constructor(props) {
    super(props);
    // What is the underlying type of this.resolver ?
    this.resolver = Promise.resolve();
  }

  handleMFACancel() {
    // Is resolver a function ?
    this.resolver(null);
  }
  handleMFASuccess(session) {
    // Is resolver a function ?
    this.resolver(session);

  }
}

I do not understand how we could use this.resolver as a function.

It is not supposed to be a resolved promise or maybe simply a promise.

Scott Sauyet
  • 49,207
  • 4
  • 49
  • 103
  • 1
    `The Promise.resolve(value) method returns a Promise object` <- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve – Ovidiu Dolha Mar 05 '18 at 13:47
  • `this.resolver()` returns a Promise object. If you want to invoke the method/function again, assign `this.resolver = Promise.resolve` (without parentheses) – wmash Mar 05 '18 at 13:50
  • Well if you just try the code you will see that it throws a type error, you cannot use a promise as a function. So yes, the code is just broken. – Bergi Mar 05 '18 at 13:50
  • Notice that they [overwrite `this.resolver` in line 59](https://github.com/awslabs/aws-mobile-react-native-starter/blob/master/client/lib/Categories/Auth/Components/Examples/SignIn.js#L59) with what is presumably a callback function. Of course all of this, especially [passing an `async function` as a `new Promise` executor](https://stackoverflow.com/a/43083793/1048572) in line 58, is just horrible horrible code. – Bergi Mar 05 '18 at 13:53
  • @Bergi you're right. I simply do not understand why it is initialized that way. –  Mar 05 '18 at 13:55
  • @OlivierMATROT Why? Because the author was clueless, or because it the code was edited and this was forgotten. It's definitely a mistake. If you want to help the project, report it as a bug - that's why it's open source. – Bergi Mar 05 '18 at 13:56
  • Why the downvote ? Please explain. –  Mar 05 '18 at 13:57
  • 1
    @Bergi I'll do that. Thanks. –  Mar 05 '18 at 13:58

1 Answers1

0

As you can see here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve Promise.resolve() returns a new Promise object

Typically this is used to create a uniform API, because the Promise returned is one that will instantly be resolved with the value provided (in your case there is no value - but you could have used Promise.resolve('some-value').

What you probably want to do here is control when cancel or success should be handled by implementing your own code to do the actual login and setting the resolver promise to your own specific logic, e.g (pseudocode):

this.resolver = http.someCallToGetSession().then(({ result, session }) => {
  if (result.val === 'ok') {
    return session;
  } else {
    throw new Error('Session not found');
  }
});
Ovidiu Dolha
  • 5,335
  • 1
  • 21
  • 30