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.
The current code
JavaScript
x
14
14
1
function getPassword(username) {
2
const password = keytar.getPassword(service, username) // Function from keytar lib
3
password.then((result) => {
4
console.log(result) // Prints password
5
return result // Doesn't return anything
6
})
7
}
8
9
pw = getPassword("Name")
10
11
// Exemplary, will be replaced by display in Div
12
console.log(pw) // Outputs "undefined"
13
14
Advertisement
Answer
JavaScript
1
11
11
1
function getPassword(username) {
2
const password = keytar.getPassword(service, username) // Function from keytar lib
3
// don't forget to return promise
4
return password.then((result) => {
5
console.log(result) // Prints password
6
return result // Doesn't return anything
7
})
8
}
9
10
getPassword("Name").then(result => console.log(result))
11