reactjsreact-nativeytdl

How to return string value?


I wrote this code

const link = "CKV0nSlxV8M"


const rendertest = async (link) => {
    const format = await ytdl(link, { quality: '22'})
    let test = JSON.stringify(format[0].url) 
        alert(test)  //string type
    return test
  
}

let finalValue = rendertest(link)
    
    
console.log(finalValue)

And I got this value from the test (string)

enter image description here

but exam value is not a String (Object)

enter image description here

I don't know which part I wrote wrong. I want the output of the same test and finalValue


Solution

  • When you define a function as async, behind the scenes it returns a promise. So, you have to wait for the promise to fulfill. Try this:

    const link = "CKV0nSlxV8M"
    
    
    const rendertest = async (link) => {
        const format = await ytdl(link, { quality: '22'});
        const test = JSON.stringify(format[0].url);
    
        return test
    }
    
    rendertest(link).then(finalValue => {
      console.log(finalValue)
    }