T
T
TheBeJIIHiu2021-12-10 13:09:35
Node.js
TheBeJIIHiu, 2021-12-10 13:09:35

Why does it return Promise { }?

const fetch = require("node-fetch");
async function gen() {
await fetch(   "https://www.reddit.com/r/memes/hot/.json?count=100"
  )
    .then(res => res.json())
    .then(json => {
      let postID =
        json.data.children[
          Math.floor(Math.random() * json.data.children.length)
        ];
      result = {
        image: postID.data.url,
        category: postID.data.link_flair_text,
        caption: postID.data.title,
        permalink: postID.data.permalink
      };
    });
  return result;
};

console.log(gen())


returns Promise { }
61b327dcb0796023287466.jpeg

Answer the question

In order to leave comments, you need to log in

5 answer(s)
V
veryoriginalnickname, 2021-12-10
@veryoriginalnickname

function gen() {
return fetch(   "https://www.reddit.com/r/memes/hot/.json?count=100"
  )
    .then(res => res.json())
    .then(json => {
      let postID =
        json.data.children[
          Math.floor(Math.random() * json.data.children.length)
        ];
      result = {
        image: postID.data.url,
        category: postID.data.link_flair_text,
        caption: postID.data.title,
        permalink: postID.data.permalink
      };
      return result;
    });
};

gen().then((e) => {
  console.log(e)
})

L
Lynn "Coffee Man", 2021-12-10
@Lynn

Because an async function always returns a Promise

V
Vasily Bannikov, 2021-12-10
@vabka

Something you made up

const fetch = require("node-fetch");
async function gen() {
    await fetch("https://www.reddit.com/r/memes/hot/.json?count=100")
        .then(res => res.json())
        .then(json => {
            let postID =
                json.data.children[
                    Math.floor(Math.random() * json.data.children.length)
                ];
            result = {
                image: postID.data.url,
                category: postID.data.link_flair_text,
                caption: postID.data.title,
                permalink: postID.data.permalink
            };
        });
    return result;
};
console.log(gen())


Try like this:
const fetch = require("node-fetch");

async function gen() {
  const response = await fetch("https://www.reddit.com/r/memes/hot/.json?count=100");
  const json = await response.json();
  const post = json.data.children[Math.floor(Math.random() * json.data.children.length)];
  return {
        image: post.data.url,
        category: post.data.link_flair_text,
        caption: post.data.title,
        permalink: post.data.permalink
      };
}

gen().then(x=>console.log(x));

A
Alexander Taratin, 2015-06-18
@Diargon345

The example uses https://github.com/Prinzhorn/skrollr

A
Alexey Rytikov, 2015-06-18
@chlp

If you have already solved the problem with a small amount of code, why do you need a plugin that will take up more space, because. universally solves problems?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question