How do I get data from a promise?

  • 0
    Don't kick hard. And so my head already hurts.
    I'm not that goofy, but promises are something ...
    Tell me how you can get the data out of a promise (as far as I understand, it's a promise)?
    There is such a thing:
    const ActiveDirectory = require('activedirectory');
    const ad_config = require('../../config/config_ad_connect');
    
    async function checkUser(login, password) {
        const ad = new ActiveDirectory(ad_config)
        await ad.authenticate(login, password, function(err, auth) {
            if (err) {
                console.log(err)
            }
            if (auth) {
                console.log(auth)
            }
        })
        return auth || err // Вот сюда надо каким-то образом засунуть то, что происходит внутри ad.authenticate
    }
    
    module.exports = checkUser

    I really studied everything that is written in the documentation on promises, async / await, callback functions. No need to advise reading. I read for three nights. The head doesn't work anymore.
    I need exactly this architecture for my function. This should be exactly a function that takes a login, a password, checks them and returns what is hidden in the promise.
    How to do it?
    JavaScript Anonymous, Oct 21, 2019

  • 1 Answers
  • 0
    No, this is not a promise, this is an asynchronous function that sends its result to a callback, as a rule, such a function does not return anything, which means there is nothing to await, since await requires a then method on the result of the expression on the right.

    But you can wrap it up in a promise:
    async function checkUser(login, password) {
    const ad = new ActiveDirectory(ad_config)
    const auth = await new Promise((resolve, reject) => {
    ad.authenticate(login, password, (err, auth) => {
    if (err) {
    return reject(err)
    }
    resolve(auth)
    })
    })
    return auth
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!