I'm using the movie database API and try to play the trailer on the youtube for that I need the key value from the JSON response. I tried everything but the function either returns a promise or undefined. I tried to use a callback to return the result but that didn't work either.
const fetch = require('node-fetch');
// function declaration
async function trailer(id, callback) {
var video = 'https://api.themoviedb.org/3/movie/' + id + '/videos?api_key=' + apiKey +'&language=en-US';
var key = await fetch(video)
.then(res => res.json())
.then(json => callback(json.results[0].key));
}
// function call returns a promise (expected key string ex."Zftx5a")
trailer(id, function(result){return result;})