0

How to get a value from a promise and store it in a variable for further use.

I am expecting my method to return a passoword as string rather than a resolved promise object. I need passoword string so that I can pass it to httpAuth function given below. httpAuth() is from TestCafe automation framework

Test.js code:

let mypass=Utils.returnPwd(); //I am using returnPwd() from Utils.js file that's returning a password.

fixture`Automated Test1`
    .meta('fixturepack', 'regression')
    .page("http://intranetURL")
    .beforeEach(async t => {             
        DriverManager.setDriver(t);
        await DriverManager.maximize();         
    })
    .httpAuth({
        username: 'loginuser1',
        password:  mypass
    })

Utils.js code:

  async base64Decoding(encodedPassword) {        
    var decodedData = Buffer.from(encodedPassword, 'base64').toString('ascii'); 
    var decodedPassword = decodedData.toString()
    console.log(decodedPassword);
    return decodedPassword;
    }

    async returnPwd(){
        let mypass2= this.base64Decoding('A2dIOEhBfXYvfSNba');        
       return mypass2.then(function(){ })
    }

Current error: credentials.password is expected to be a string, but it was object.

.httpAuth({ username: 'loginuser1', password: mypass })

Swami
  • 3
  • 2
  • 2
    Why is `base64Decoding` declared as `async`? I don't see anything in there that would require that. Same goes for `returnPwd` –  May 31 '21 at 19:16
  • @Barmar Not sure that's a fitting duplicate? –  May 31 '21 at 19:18
  • Declaring a function `async` makes it return a Promise. Why did you expect it to return the password instead? You can use `let mypass = await Utils.returnPwd()` if the caller is also an `async` function, otherwise you have to resolve the promise. – Barmar May 31 '21 at 19:25
  • @Barmar This question isn't *really* about Promises though. Neither of these methods is supposed to be `async` in the first place, and removing the keyword from both will immediately fix the problem. –  May 31 '21 at 19:51
  • @ChrisG That's true, I didn't really look at what the functions are doing. – Barmar May 31 '21 at 20:00
  • Thank you @Barmar ,Chris G ,Rajdeep Debnath . Issue resolved after making the method as synchronous. – Swami Jun 01 '21 at 02:28

1 Answers1

1

I did not see any async code in Untils.js, you can make it synchronous

//Test.js
let mypass = Utils.returnPwd();

//Utils.js
base64Decoding(encodedPassword) {        
    var decodedData = Buffer.from(encodedPassword, 'base64').toString('ascii'); 
    var decodedPassword = decodedData.toString()
    console.log(decodedPassword);
    return decodedPassword;
    }

    returnPwd(){
        let mypass2= this.base64Decoding('A2dIOEhBfXYvfSNba');        
       return mypass2;
    }
Vivek Bani
  • 3,703
  • 1
  • 9
  • 18