I am trying to access [[Promise Results]] and save it as a variable. The end goal I just want the result from the .then statement and use it in other function. If there is another better way to do it please let me, I'm new to JavaScript, so it would be awesome if you could explain it to me than just dump code. Thanks is advance Here is the fetch request Show
The code below is when I console log the function in another function
Qiniso 2,4571 gold badge23 silver badges29 bronze badges asked Oct 2, 2020 at 1:01
6 There are 3 ways for solving this:
answered Oct 2, 2020 at 1:27
Omri AttiyaOmri Attiya 3,7843 gold badges19 silver badges34 bronze badges 2 Instead of promise chains you could use
answered Oct 2, 2020 at 1:21
JasonJason 3291 silver badge8 bronze badges You can call then() on the same promise as many times as you want. Storing the
promise returned by the function in a variable allows you to use
answered Oct 2, 2020 at 1:39
charlietflcharlietfl 170k13 gold badges115 silver badges146 bronze badges 2 Not the answer you're looking for? Browse other questions tagged javascript ecmascript-6 promise or ask your own question.Originally posted on my personal blog. Intro (completely off-topic)It's has been almost 3 months since my last blog post. There are reasons for that. First, despite all precautions, I got sick with coronavirus (COVID-19) in the second half of June 2020. For two weeks it was total hell. Very bad well-being, I could only lie in bed and hope that it will go away soon. After that, it was a recovery for the next 2-3 weeks. Now I'm finally got back to normal life and even resumed my fitness training. So, coronavirus is no joke. Please, stay safe. Second, there are lots of things happening right now in my home country - Belarus. Belarussians are fighting against dictatorship. Our (ex)-president lost last elections which were held on August 9th, 2020, but he continues to stay in power using brutal police and army forces against peaceful people and to threaten to anybody who disagrees with him. But we keep on fighting and to protest every day. I do take all these events very close to heart and hope to wake up one day in a free, democratic, and prosperous Belarus. Now back to the topic. What is a Promise in JavascriptA Promise is an object representing the eventual completion or failure of an asynchronous operation. A Promise may be in one of the following states:
One of the most widely used examples of asynchronous operations in Javascript is a Fetch API. The fetch() method returns a Promise. Assume that we fetch some data from a backend API. For this blog post, I'll use JSONPlaceholder - a fake REST API. We will fetch a user's data with the id = 1:
Enter fullscreen mode Exit fullscreen mode Let's see how we can access returned data. 1 - .then() chainingIt is the most simple and the most obvious way.
Enter fullscreen mode Exit fullscreen mode Here we (1) fetch data from the API, (2) transform it into JSON object and then (3) print user's address value to the console. The result is:
Enter fullscreen mode Exit fullscreen mode 2 - Use returned value later in a codeBut what if we'd like to use the returned value somewhere later in code? If we try to do it
like this (wrong way!):
Enter fullscreen mode Exit fullscreen mode We'll get
Enter fullscreen mode Exit fullscreen mode It's happening because the Javascript code always executes synchronously, so the console.log() function starts immediately after the fetch() request, not waiting until it is resolved. In the moment when console.log() function starting to run, a Promise that should be returned from a fetch() request is in a pending status. That said we can
access the returned value of a Promise object in another .then() callback:
Enter fullscreen mode Exit fullscreen mode OR using async / await syntax:
Enter fullscreen mode Exit fullscreen mode In both ways, we'll get:
Enter fullscreen mode Exit fullscreen mode ConclusionA Promise object is widely used in Javascript async programming. And it's sometimes confusing for developers how to use it properly. In this blog post, I've attempted to describe a use case when a developer needs to use a returned value from a Promise object somewhere later in code. |