Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6212

Cannot receive image url from this async function

$
0
0

I am trying to get the image url from itunes. This is the code:

interface SongProps {  data: {    name: string;    artist: string;    album: string;    at: string;  };}const ITUNES_ENDPOINT ="https://itunes.apple.com/search?limit=1&&media=music&limit=1&term=";async function artWork(name:string, artist:string): Promise<string | null> {  return new Promise((resolve) => {    if (name.length === 0) {      resolve(null);      return    }    const searchTerm = `${artist} ${name ? name : artist}`;    fetch(`${ITUNES_ENDPOINT}${encodeURI(searchTerm)}`)      .then((response) => response.json())      .then((data) => {        if (data.results.length > 0) {          const res = data.results[0];          resolve(res.artworkUrl100.replace("100x100", "600x600"));        }        resolve(null)        return null      })      .catch((e) => console.error("Critical failure: "+ e.message));  });}const src = (props:SongProps)=>{  return [  artWork(props.data.name,props.data.artist)]};const FinalImg = (props:{src:string | number})=>{  console.log(src)  return (<View style={styles.albumArtContainer}><Image source={src} style={styles.albumArt} /></View>  )}

and my result in src shout be src="https://is4-ssl.mzstatic.com/image/thumb/Music30/v4/a0/08/ee/a008eecc-f4e8-4eec-e293-95aed1673a54/source/100x100bb.jpg";I got no errors.
Any advice?
P.S.: I am a newbie in ts


Viewing all articles
Browse latest Browse all 6212

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>