Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8017019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:42:44+00:00 2026-06-04T20:42:44+00:00

I am writing a function which has to get the thumbnail information from a

  • 0

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:

  1. the function is called
  2. undefined is returned
  3. XHR request is finished
  4. variable thumbnail content is shown in console

This is obviously the wrong order.

Any help is greatly appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:42:47+00:00Added an answer on June 4, 2026 at 8:42 pm

    Updated answer

    getJSON returns a promise (a read-only deferred), so you can listen to it. But since you need some post-processing, you’d want to chain a then which allows you to alter the resolved value.

    // Now using `then`
    function getThumbnail(vUrl){
      return $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl).then(function(data){
        return {
          thumbnail:data.thumbnail_url,
          vurl:vurl
        }
      });
    }
    
    //and in your call will listen for the custom deferred's done
    getThumbnail('the_vurl_').then(function(returndata){
      //received data!
    });
    

    Original answer

    You can use a deferred object, and listen for the done().

    function getThumbnail(vUrl) {
        //create our deferred object
        var def = $.Deferred();
    
        //get our JSON and listen for done
        $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl)
            .done(function(data){
    
                //resolve the deferred, passing it our custom data
                def.resolve({
                    thumbnail:data.thumbnail_url,
                    vurl:vurl
                });
            });
    
        //return the deferred for listening
        return def;
    }
    
    //and in your call will listen for the custom deferred's done
    getThumbnail('the_vurl_')
        .done(function(returndata){
            //received data!
        });
    

    You could return $.getJSON‘s deferred to get the raw data. But because of “post-processing” into an object, the custom deferred is needed. You could also pass a callback to getThumbnail():

    function getThumbnail(vUrl,callback) {
        $.getJSON("http://api.embed.ly/1/oembed?key=:key&url="+vurl,function(returndata){
            callback(returndata);
        });
    }
    
    getThumbnail('the_vurl_',function(returndata){
        //received data!
    })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a function which would get all files from a particular directory
I am writing a Function that has a return-type which will dynamically change depending
I am writing a function which has many return points (depending on what conditions
I am writing a function in which I need to read a string contains
I've been writing a javascript function which returns true if the value matches one
I'm writing a basic function in PHP which takes an input string, converts a
I'm writing a function intended to sanitize a poorly formatted string which the php
I am writing a functor F which takes function of type void (*func)(T) and
I am writing down a function and it takes an argument which is the
I am writing a code, in which one of the files uses set_intersection function,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.