1

I have written this script for getting thumbnail from vimeo. But getting this result as promise Object. Actually, I want this result a thumbnail.

 let url ='https://vimeo.com/535319285';
const getVimeoThumbnail =  (url) => {
    if (url) {
        var video_id, thumbnail, result;
        if (url.match(/https?:\/\/(www\.)?vimeo.com\/(\d+)($|\/)/)) {
            video_id = url.split('/')[3];
        }
        else if (url.match(/^vimeo.com\/channels\/[\d\w]+#[0-9]+/)) {
            video_id = url.split('#')[1];
        }
        else if (url.match(/vimeo.com\/groups\/[\d\w]+\/videos\/[0-9]+/)) {
            video_id = url.split('/')[4];
        }
        else if (url.match(/player.vimeo.com\/video\/[0-9]+/)) {
            video_id = url.split('/')[2];
        }
            result = new Promise((resolve, reject) => {
                fetch('https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/' + video_id)
                    .then(response => {
                        response.json().then((jsonData) => {
                            thumbnail = jsonData.thumbnail_url;
                             resolve(thumbnail);
                        }).catch((error) => {
                            console.error(error);
                             reject(false);
                        })
                    });
            });
          
             console.log("Result ::", result);
             return result;
       
    }
}

enter image description here

  • You get the thumbnail by using the promise. Details in [the linked question](https://stackoverflow.com/questions/29516390/how-to-access-the-value-of-a-promise)'s answers. – T.J. Crowder Apr 21 '21 at 10:15
  • just add `return` before `new...` keyword : `result = return new Promise((resolve, reject) => {` – Dhaval Darji Apr 21 '21 at 10:23
  • @DhavalDarji can you write some more details about what u are trying to say. – Dheeraj Kumar Apr 21 '21 at 10:25
  • `result = return new Promise((resolve, reject) => {` Compare this with your existing code line, you'll see the `return` keyword in my statement. @DheerajKumar – Dhaval Darji Apr 21 '21 at 10:27
  • @DhavalDarji I have added code this way still getting same result : return new Promise((resolve, reject) => { fetch('https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/' + video_id) .then(response => { response.json().then((jsonData) => { thumbnail = jsonData.thumbnail_url; resolve(thumbnail); }).catch((error) => { console.error(error); reject(false); }) }); }); – Dheeraj Kumar Apr 21 '21 at 10:30
  • @T.J.Crowder I am following the link that you have shared. But still could not be able to get desired result. – Dheeraj Kumar Apr 21 '21 at 10:32
  • @DheerajKumar - See also [the other question's answers](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). It doesn't matter how many `.then`s you add. You can't use the result before you have it, which means you can't **return** it from your function. You have to return the promise, and have the code using your function hook the fulfillment of that promise and *then* use the result. – T.J. Crowder Apr 21 '21 at 10:37

0 Answers0