Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable of function always returns undefined

The idea:

I want to return a variable from a function and then output it using console.log().

The problem:

I can’t just use return result because then nothing is returned.

I don`t really know how else to return the variable.

I have already looked at SO posts like this one, however I probably lack suitable understanding to implement this into my code.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The current code

function getPassword(username) {
    const password = keytar.getPassword(service, username) // Function from keytar lib
    password.then((result) => {
        console.log(result)         // Prints password
        return result               // Doesn't return anything
    })
}

pw = getPassword("Name")

// Exemplary, will be replaced by display in Div
console.log(pw)                     // Outputs "undefined"
    

>Solution :

function getPassword(username) {
    const password = keytar.getPassword(service, username) // Function from keytar lib
    // don't forget to return promise
    return password.then((result) => {
        console.log(result)         // Prints password
        return result               // Doesn't return anything
    })
}

getPassword("Name").then(result => console.log(result))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading