Is this code correct? Can I combine promises like that?
var data = {}
await getInfo(data.com)
 .then(result => {data = result})
 .catch(error => {})
doStuffOnlyAfterGetInfo(data)
                Yes, you can do that, you can think of await like this:
await x where x is a Promise OR any value. In your case that chain call returns a promise.
all of this works:
async function fn() {
  try {
    var a = await 20; // a = 20
    var b = Promise.resolve(20); // b = 20
    var c = Promise.reject(20); // This throws exception
    var d = Promise.reject(20).catch(() => 30) // d = 30
    var e = fetch(url) // same as fetch(url).then(e => /*...*/)
    var f = fetch(url).then(res => res.json())
  catch (e) {
    console.log(e) // 20
  }
}
Also don't forget, that you can only use await in async functions.
Seed the Docs
You can, since promise.then() and promise.catch() return promises as well.
You can do the same without then\catch using try\catch
async function getInfo(url)
{
   try{
     const result = await fetch(url);
     return {data: result};
   }
   catch(e){
   }
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With