Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 142382

How can I access the value returned by a promise?

$
0
0

i'm kind of new to stackoverflow and i've been dealing for days with a problem. I have the next piece of code:

let comptot = function (value, data) {
     return fetch(API_TOT)
    .then(response => response.json())
    .then((data) => {
        let x = data[0].cantidad;
        console.log(x);
        return x;
    })
    .catch(error => {
        console.log("el error es el siguiente", error)
    })}

The problem is I can't access the value returned by it. It does log the value (230) to the console, but I want to display that value to a table (I'm using Tabulator), and it only returns:

Promise {<pending>}
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: "230"

I've read a bunch of quite similar questions but I can't figure out how to solve it. I also kind of understand how promises work, but obviously I didn't understood everything or I wouldn't be getting this problem (I also read a number of articles about promises and watched tens of youtube videos about promises and still nothing). I also tried using Async Await with the following code and had exactly the same problem:

let comtot = async function (value, data) {
    let response = await fetch(API_TOT);
    let com = await response.json();

    console.log(com[0].cantidad);
    return com[0].cantidad;
}

Please help me solve this, I would really apreciate it!


Viewing all articles
Browse latest Browse all 142382

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>