0

I'm currently trying to use some code from a fellow user on GitHub. It's goal is to wrap around the API of a specific website.

Right now the code does this:

const rp = require('requests-promise');
function login(user, pass) {
    var hash = md5(`${user}-${md5(pass)}`);
    var options = {
        method: 'POST',
        uri: opts.url + 'Login.json',
        form: {
            Username: user,
            Hash: hash
        },
        json: true // Automatically stringifies the body to JSON
    };

    return rp(options).then(j => {
            if (j.Status) {
                self.loginData = j;
                if (j.Notifications.length > 0) {
                    console.log("User has a new notifications: " + j.Notifications.join(", "));
                }
                delete(self.loginData.Status);
                delete(self.loginData.Notifications);
            } else {
                throw new Error("Bad login.");
            }
            return Boolean(j.Status)); // fail-sucess
        })
    })
}

How do I get the return from the promise to be the return value of the login function? Currently it just returns the promise object instead of the intended value which is a Boolean to correspond to the login state.

wolfy1339
  • 784
  • 2
  • 7
  • 23
  • You cannot return the future result immediately. You need to change your caller to use the promise. – Bergi Nov 14 '17 at 22:05
  • `Currently it just returns the promise object` yes, that's how promises work .. you need to change the code that calls login function, `login(......).then(result => result will be what you want)` – Jaromanda X Nov 14 '17 at 22:05

0 Answers0