2

I am trying to expand my knowledge (beginner stage). Basically, I would like to use promises to write new email to my user. I have some code base in my play ground project, but my function is not stopping on then. This is the function that should write to Database:

changeEmailAddress(user, newEmail) {
       new Promise((resolve, reject) => {
            user.setEmail(newEmail);
            userRepository.saveUser(user).then(() => {
                return resolve();
            }).catch(e => {
                return reject(e);
            });
        }
    );
}

And if I am not mistaken, this is how I should use it:

changeEmailAddress(user, "hello@there.com").then(function () {

    //it never comes in here :(
})

I have similar functions working on the user, but my function is not coming in to 'then'

Salko Crni
  • 45
  • 7
  • 1
    If this is your actual code, then the `changeEmailAddress(...).then(` part should generate a run-time error such as "undefined.then is not a function". Does it? That would have been a **big** hint as to what was going wrong. –  May 31 '17 at 10:47
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi May 31 '17 at 13:49

3 Answers3

5

You're committing the explicit promise constructor anti-pattern. Your code need be no more complicated than

changeEmailAddress(user, newEmail) {
  user.setEmail(newEmail);
  return userRepository.saveUser(user);
}

Making sure, of course, to not forget the return!

-2

your function changeEmailAddress return nothing, that's why

changeEmailAddress(user, newEmail) {
   let promise = new Promise((resolve, reject) => {
        user.setEmail(newEmail);
        userRepository.saveUser(user).then(() => {
            return resolve();
        }).catch(e => {
            return reject(e);
        });
    });
    return promise;
}
Yordan Nikolov
  • 2,598
  • 13
  • 16
-2

You are not returning the new Promise.

changeEmailAddress(user, newEmail) {
    return new Promise((resolve, reject) => {
        user.setEmail(newEmail);
        userRepository.saveUser(user).then(() => {
            resolve();
        }).catch(e => {
            reject(e);
        });

    });
}

You may also have an unhandled rejection.

changeEmailAddress(user, "hello@there.com").then(function () {
    //it never comes in here :(
}).catch(function(e) {
    console.log(e); // Does this happen?
})

EDIT: Your changeEmailAddress uses the anti-pattern (see @torazburo's answer). Although this answer works, you should just return your saveUser promise unless you want to directly work with the result of it.

Jim Wright
  • 5,905
  • 1
  • 15
  • 34
  • 1
    I hane not tested does it stop there. I need to handle that somehow. – Salko Crni May 31 '17 at 09:34
  • 2
    This answer, including the code in the question is implementing the anti-pattern that torazaburo mentions in his answer; his answer is the one that should be marked as an answer not to promote the use of anti-patterns. – Daniel B May 31 '17 at 10:59