I'm aware that there are hundreds of thousands of threads on this subject already out there and I've been studying the problem for a few days now, but I still don't understand how to apply the examples that I've gone through to my case.
I start with an API call like this:
const getReleaseUrl = (discogsId) => {
db.getRelease(discogsId, (err, data) => {
return data.uri
})
}
console.log(getReleaseUrl("53130"));
// => undefined
How can I make sure that my app receives the data first, prevents the undefined
return? Do I need to somehow make sure that db
gets defined and connected to the remote database with my client id first? Do I need to write a function that somehow catches the undefined
result and keeps running the API call over and over until the result is something different? Setting a timeout does not seem like a good permanent solution. I tried writing a Promise chain like this and got the TypeError: db.getRelease(...).then is not a function
error:
const getReleaseUrl = (discogsId) => {
db.getRelease(discogsId, (err, data) => {
return data
})
.then((data) => {
return data.uri
})
}
console.log(getReleaseUrl("53130"));