1

maybe the is pretty stupid question but what is the way to go here? I would like to create a function which returns to fulfilled promises from other functions?

getStorageData(){
    var result = []
    this.storage.get('ipaddress').then(ip => {
        result[0] = ip
        this.storage.get('timestamp').then(timestamp => {
            result[1] = timestamp
            console.log(result)
    })})
    return result
}

the console output is fine but the returned value of my defined function is an empty array. How do I get the output of the console to be the returned value of my function?

Thanks!

Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
Jan Peter
  • 33
  • 6

1 Answers1

0

This is a asyncrounous function:

this.storage.get('timestamp').then(timestamp => {
    result[1] = timestamp
    console.log(result)
}

Your script needs some time to get to console.log(result). So the result is not defined at the place where you want to return it.

You could return it in the function body of the .then() function like this:

this.storage.get('timestamp').then(timestamp => {
    result[1] = timestamp;
    console.log(result);
    return result;
}

Note: Don't forget the ; at the end of a command.

Ma Kobi
  • 888
  • 6
  • 21