1
const Video = require("");
const token = "";
const connectOptions = {logLevel: "off"}
 
const startRoom = function(token) {
 console.log("hello world");
 Video.connect(a)
   .then(room => null
   })
   .catch(error => {
     console.log("error");
     return error
   });
}

The async/await will lead to removal of catch. Which is what I want to achieve.

Shivangi
  • 95
  • 1
  • 2
  • 9
  • 1
    Does this answer your question? [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Igor Aug 07 '20 at 15:25
  • See the answer in the suggested duplicate, heading `ES2017+: Promises with async/await`. That should answer your question. – Igor Aug 07 '20 at 15:25
  • Promises are core behind async-await, please be more clear what you want to acheive, it seems to me like a [`XY question`](https://en.wikipedia.org/wiki/XY_problem#:~:text=The%20XY%20problem%20is%20a,them%20to%20resolve%20issue%20X.) – Kunal Mukherjee Aug 07 '20 at 15:30

3 Answers3

1

Just fyi, you're not using await INSTEAD of promises, you're using await WITH promises. async functions return promises, and await waits for promises to resolve

const Video = require("twilio-video");
const token = "test_token";
const connectOptions = {video: false, audio: false, logLevel: "off"}
 
const startRoom = async function(token) {
 console.log("hello world");
 try {
   const room = await Video.connect(token, connectOptions)
   console.log("got a room");
 } catch(error) {
   console.log("error");
 }
}
TKoL
  • 13,158
  • 3
  • 39
  • 73
0

Just wrap it in try-catch

const getRoom = async () => {
    try {
        const room = await Video.connect(token, connectOptions);
        console.log("got a room");
    } catch (e) {
        console.log("error");
    }
}
David ZIP
  • 26
  • 4
0

or you can use this also..

async function startRoom(token) {
  console.log("hello world");
  try {
    let response = await fetch(token);
    if (!response.ok) {
      throw new Error(`HTTP error! status: ${response.status}`);
    } else {
      let room = await video.connect(token, connectOptions);
      console.log("got a room");
    }
  } catch (e) {
    console.log(e);
  }
}