-2

Ive read plenty of articles on Promises and I feel i'm still missing something. Here is an example of what i'm trying to wrap my head around.

File: ad.js

// Using node.js activedirectory

class AD
{
    constuctor(options){
        this.ad = new ActiveDirectory(options);
    }

    isUserValid(user, password){
        return new Promise((resolve, reject) =>{            
            ad.authenticate(user, password, (err, auth) ->{
                if(err){
                    reject({ code: 500, message: 'Unknow error'});
                }

                if(auth){
                    resolve({code: 200, message: 'ok'});
                }           
            });
        )
    }
}

module.exports = {
    AD: AD
}


File: session.js

createSession(user, password){
    var self = this;

    return new Promise((resolve, reject) =>{
        const ad = new AD("Required parameters"); 

        Code: 1
        const result = self._isADValid(ad, user, password);

        Code: 2
        self._isADValidPromise(ad, user, password)
        .then(resp_ad =>{
        })
        .catch(err =>{
        }); 
    );
}

_isADValidPromise(ad, user, password) {
    return new Promise((resolve, reject) => {
        ad.isUserValid(user, password)
            .then(resp_ad => {
                resolv(resp_ad);
            })
            .catch(err => {
                reject(err);
            });
    });
}
_isADValid(ad, user, password) {
    return ad.isUserValid(user, password)
        .then(resp_ad => {
            return resp_ad;
        })
        .catch(err => {
            return err;
        });
}

What i'm trying to understand is the following:

Shouldnt "Code 1" return a value from _isADValid. Instead its returning "Promise{ pending }". I though that you only need to use then/catch from a Promise? So the function _isADValid() is calling the AD function isUserValid which is returning from a Promise and _isADValid() is wrapped in a then/catch which just returns the result.

Code 2 using "_isADValidPromise" works without any issues. And I get that its wrapped in a Promise so its doing what I expected.

If anyone can help clarify why Code 1 is not working

adviner
  • 3,295
  • 10
  • 35
  • 64

1 Answers1

1

You have to remember that Promises don't resolve until (at least) the next tick (details here). You're expecting the Promise to return synchronously, but Promises are by definition asynchronous.

Your example demonstrates this: If you think about it, your call to authenticate is very likely asynchronous - reaching out to some server to authenticate the user. Promises were invented to wrap asynchronous operations such as this one in a way that provides a nice control flow and avoids callback hell. So in short, any time you use a Promise, it will return asynchronously.

In that light, self._isADValid(ad, user, password); returns a Promise, so naturally, result is a Promise. The only way to get the result out of it is to add a .then() and wait for it to resolve, which will always be at least one tick into the future.

Hope that helps.

Community
  • 1
  • 1
Ian
  • 3,806
  • 2
  • 20
  • 23
  • Taking what you wrote and making more examples to clarify help me clarify it. Thank you for the response – adviner Mar 06 '17 at 22:13