How to fetch and store [[PromiseValue]] into local variable?










0















There are two functions; function getThumbnail(url) and function getVimeoData() .



I'm using fetch() to handle the HTTP calls for retrieving the url of thumbnail of a video in Vimeo. oEmbed is used to fetch JSON data of a video, which contains thumbnail_url.



The functions goes as follow:



async function getVimeoThumb(videoUrl) 
return await fetch(videoUrl)
.then(response =>
if (response.status !== 200)
console.log('Error occurred while fetching thumbnail.');
return null;


return response.json();
)
.then(data => return data['thumbnail_url'])
.catch(err => console.log(err.reason));



The above function returns Promise with a value [[PromiseValue]]: "thumbnail_url". The returned Promise value is fetched in following function as:



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => console.log(result))

...



The above function returns console log as thumbnail_url.



Hence, my question is that is there any workaround to store the fetched value thumbnail_url in my local variable newUrl?










share|improve this question






















  • Possible duplicate of How do I return the response from an asynchronous call?

    – str
    Nov 14 '18 at 9:27















0















There are two functions; function getThumbnail(url) and function getVimeoData() .



I'm using fetch() to handle the HTTP calls for retrieving the url of thumbnail of a video in Vimeo. oEmbed is used to fetch JSON data of a video, which contains thumbnail_url.



The functions goes as follow:



async function getVimeoThumb(videoUrl) 
return await fetch(videoUrl)
.then(response =>
if (response.status !== 200)
console.log('Error occurred while fetching thumbnail.');
return null;


return response.json();
)
.then(data => return data['thumbnail_url'])
.catch(err => console.log(err.reason));



The above function returns Promise with a value [[PromiseValue]]: "thumbnail_url". The returned Promise value is fetched in following function as:



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => console.log(result))

...



The above function returns console log as thumbnail_url.



Hence, my question is that is there any workaround to store the fetched value thumbnail_url in my local variable newUrl?










share|improve this question






















  • Possible duplicate of How do I return the response from an asynchronous call?

    – str
    Nov 14 '18 at 9:27













0












0








0








There are two functions; function getThumbnail(url) and function getVimeoData() .



I'm using fetch() to handle the HTTP calls for retrieving the url of thumbnail of a video in Vimeo. oEmbed is used to fetch JSON data of a video, which contains thumbnail_url.



The functions goes as follow:



async function getVimeoThumb(videoUrl) 
return await fetch(videoUrl)
.then(response =>
if (response.status !== 200)
console.log('Error occurred while fetching thumbnail.');
return null;


return response.json();
)
.then(data => return data['thumbnail_url'])
.catch(err => console.log(err.reason));



The above function returns Promise with a value [[PromiseValue]]: "thumbnail_url". The returned Promise value is fetched in following function as:



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => console.log(result))

...



The above function returns console log as thumbnail_url.



Hence, my question is that is there any workaround to store the fetched value thumbnail_url in my local variable newUrl?










share|improve this question














There are two functions; function getThumbnail(url) and function getVimeoData() .



I'm using fetch() to handle the HTTP calls for retrieving the url of thumbnail of a video in Vimeo. oEmbed is used to fetch JSON data of a video, which contains thumbnail_url.



The functions goes as follow:



async function getVimeoThumb(videoUrl) 
return await fetch(videoUrl)
.then(response =>
if (response.status !== 200)
console.log('Error occurred while fetching thumbnail.');
return null;


return response.json();
)
.then(data => return data['thumbnail_url'])
.catch(err => console.log(err.reason));



The above function returns Promise with a value [[PromiseValue]]: "thumbnail_url". The returned Promise value is fetched in following function as:



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => console.log(result))

...



The above function returns console log as thumbnail_url.



Hence, my question is that is there any workaround to store the fetched value thumbnail_url in my local variable newUrl?







javascript fetch es6-promise vimeo-api






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 9:15









Pa-wonPa-won

6517




6517












  • Possible duplicate of How do I return the response from an asynchronous call?

    – str
    Nov 14 '18 at 9:27

















  • Possible duplicate of How do I return the response from an asynchronous call?

    – str
    Nov 14 '18 at 9:27
















Possible duplicate of How do I return the response from an asynchronous call?

– str
Nov 14 '18 at 9:27





Possible duplicate of How do I return the response from an asynchronous call?

– str
Nov 14 '18 at 9:27












2 Answers
2






active

oldest

votes


















0














You already have a local variable (newUrl), so where you are logging the result you can save there only



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...






share|improve this answer


















  • 1





    While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

    – str
    Nov 14 '18 at 9:28











  • I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

    – Nitish Narang
    Nov 14 '18 at 10:49



















0














@Nitich is right. But if what you want is use newUrl out side you can try this. newUrl will be in the global scope.



 let newUrl;
function getThumbnailUrl()

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...



Or simply do



 ...

getVimeoThumb(url).then(result => newUrl = result; return newUrl; )

...
}
let newUrl = getThumbnailUrl(videoUrl);


I hope this helps.






share|improve this answer

























  • No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

    – str
    Nov 14 '18 at 10:45










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296596%2fhow-to-fetch-and-store-promisevalue-into-local-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You already have a local variable (newUrl), so where you are logging the result you can save there only



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...






share|improve this answer


















  • 1





    While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

    – str
    Nov 14 '18 at 9:28











  • I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

    – Nitish Narang
    Nov 14 '18 at 10:49
















0














You already have a local variable (newUrl), so where you are logging the result you can save there only



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...






share|improve this answer


















  • 1





    While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

    – str
    Nov 14 '18 at 9:28











  • I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

    – Nitish Narang
    Nov 14 '18 at 10:49














0












0








0







You already have a local variable (newUrl), so where you are logging the result you can save there only



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...






share|improve this answer













You already have a local variable (newUrl), so where you are logging the result you can save there only



function getThumbnailUrl() 
let newUrl;

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 9:18









Nitish NarangNitish Narang

2,9401815




2,9401815







  • 1





    While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

    – str
    Nov 14 '18 at 9:28











  • I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

    – Nitish Narang
    Nov 14 '18 at 10:49













  • 1





    While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

    – str
    Nov 14 '18 at 9:28











  • I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

    – Nitish Narang
    Nov 14 '18 at 10:49








1




1





While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

– str
Nov 14 '18 at 9:28





While this might work in theory, it is not usable in practice. You don't know when newUrl is actually defined. The correct answer to this question can be found in the duplicate.

– str
Nov 14 '18 at 9:28













I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

– Nitish Narang
Nov 14 '18 at 10:49






I get that @str . Though not clearly stated in OP that async is causing trouble or not getting proper response from async calls rather it was menitioned that results are coming just needed to save to local variable. Still it looks your insight could be correct on this. Thnx

– Nitish Narang
Nov 14 '18 at 10:49














0














@Nitich is right. But if what you want is use newUrl out side you can try this. newUrl will be in the global scope.



 let newUrl;
function getThumbnailUrl()

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...



Or simply do



 ...

getVimeoThumb(url).then(result => newUrl = result; return newUrl; )

...
}
let newUrl = getThumbnailUrl(videoUrl);


I hope this helps.






share|improve this answer

























  • No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

    – str
    Nov 14 '18 at 10:45















0














@Nitich is right. But if what you want is use newUrl out side you can try this. newUrl will be in the global scope.



 let newUrl;
function getThumbnailUrl()

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...



Or simply do



 ...

getVimeoThumb(url).then(result => newUrl = result; return newUrl; )

...
}
let newUrl = getThumbnailUrl(videoUrl);


I hope this helps.






share|improve this answer

























  • No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

    – str
    Nov 14 '18 at 10:45













0












0








0







@Nitich is right. But if what you want is use newUrl out side you can try this. newUrl will be in the global scope.



 let newUrl;
function getThumbnailUrl()

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...



Or simply do



 ...

getVimeoThumb(url).then(result => newUrl = result; return newUrl; )

...
}
let newUrl = getThumbnailUrl(videoUrl);


I hope this helps.






share|improve this answer















@Nitich is right. But if what you want is use newUrl out side you can try this. newUrl will be in the global scope.



 let newUrl;
function getThumbnailUrl()

...

getVimeoThumb(url).then(result => newUrl = result; console.log(result) )

...



Or simply do



 ...

getVimeoThumb(url).then(result => newUrl = result; return newUrl; )

...
}
let newUrl = getThumbnailUrl(videoUrl);


I hope this helps.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 10:20

























answered Nov 14 '18 at 10:14









Kareem_BincomKareem_Bincom

161




161












  • No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

    – str
    Nov 14 '18 at 10:45

















  • No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

    – str
    Nov 14 '18 at 10:45
















No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

– str
Nov 14 '18 at 10:45





No he is not quite right. Your first example has the same limitations as his. And the second example will return a promise, not the URL.

– str
Nov 14 '18 at 10:45

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53296596%2fhow-to-fetch-and-store-promisevalue-into-local-variable%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto