синхронные звонки (ожидайте объявления. аутентификация) - PullRequest
0 голосов
/ 05 ноября 2019

Как я могу сделать асинхронный вызов? я пробовал с этим кодом:

abc = await ad.authenticate(username, password, function(err, auth) {
          if(err){
            console.log('ERROR: '+JSON.stringify(err));
            fail_found = err.name;
            return;
          }

          if(auth){
            console.log('Authenticated!');
          }else{
            console.log('Authenticated Faliled');
            fail_found = 'No Authenticated ';
          }          
        });
      }
      console.log("Fails?: ",fail_found);

NPM: https://www.npmjs.com/package/activedirectory2 Github: https://github.com/jsumners/node-activedirectory/

Thx!

1 Ответ

1 голос
/ 05 ноября 2019

Вы можете попробовать С Обещанием :

let myPromise = new Promise((resolve, reject) => {
     ad.authenticate(username, password, function(err, auth) {
      if(err){
        console.log('ERROR: '+JSON.stringify(err));
        fail_found = err.name;
        reject(fail_found)
        return;
      }

      if(auth){
        console.log('Authenticated!');
        resolve('Authenticated!');
      }else{
        console.log('Authenticated Faliled');
        fail_found = 'No Authenticated ';
        reject(fail_found)
      }          
    });
  }
  console.log("Fails?: ",fail_found);
  reject(fail_found)
});

myPromise.then((successMessage) => {
 console.log(successMessage);
}).catch(err => console.log(err))

Или в первой части документации показать обещание Обертки :

const AD = require('activedirectory2').promiseWrapper;
const config = { url: 'ldap://dc.domain.com',
           baseDN: 'dc=domain,dc=com',
           username: 'username@domain.com',
           password: 'password' }
const ad = new AD(config);
...