I am writing a function which has to get the thumbnail information from a given video using the embed.ly API, however currently the function returns a value before it even got the JSON result from the API.
I am using the following code:
function getThumbnail(vUrl) {
var thumbnail = '';
var title = '';
var caption = '';
var content = '';
$.when( $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl) ).then(function(data){
var thumbnail = data.thumbnail_url;
console.log(thumbnail);
return {
thumbnail:thumbnail,
vurl:vurl
}
});
}
However when using the Chrome Javascript console I can see that:
- the function is called
- undefined is returned
- XHR request is finished
- variable thumbnail content is shown in console
This is obviously the wrong order.
Any help is greatly appreciated!