#How to call a another function that handles a promise but await for its value

15 messages · Page 1 of 1 (latest)

plush leaf
#

Hey all, I have a function thats similar to this

function func(){
 navigator.permissions.query({name:"geolocation"})
        .then((result)=>{
        .catch(...)
}
//in all cases it returns some object {a:"1", b:"2"} 


function anotherfunc(){
  const funcResults = func() // i cant get it to await here
  console.log(funcResults) //either uncaught (in promise) error or undefined

  hmtltag.innerHTML = funcResults.b
}
#

Im having a lot of trouble actually awaiting for the func function to resolve

#

I want the steps of anotherfunc to fully block until func is done exectuing

manic spire
#

don't use .then

#

use async and await

plush leaf
#

i use .then to help create the output object depending what the result of the navigator promise is

manic spire
#
async function func() {
  try {
    const result = await navigator.permissions.query({name:"geolocation"})
    ...
  } catch {
    ...
  }
}
plush leaf
#

ahhh

manic spire
#

await is better than .then(), hands down

#

don't use .then()

plush leaf
#

thanks ill give it a go

#

appreciate it

manic spire
#

no problem!

plush leaf
#

@manic spire So much easier and more logical than .then and .catch it seems like

#

worked like a charm