TheBeJIIHiu
@TheBeJIIHiu
Просто обычный кодер?

Почему возвращает Undifined?

const fetch = require("node-fetch");
let meme;
//import fetch from 'node-fetch';
const fs = require("fs");
function gen() {
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)
        ];
      this.result = {
        image: postID.data.url,
        category: postID.data.link_flair_text,
        caption: postID.data.title,
        permalink: postID.data.permalink
      };
    });
  return this.result;
};

console.log(gen())


Почему возвращает Undifined?
  • Вопрос задан
  • 69 просмотров
Пригласить эксперта
Ответы на вопрос 1
@Azperin
Дилетант
Асинхронность. Думаю такой пример будет понятен
function gen() {
	setTimeout(() => {
		this.result = 'SOMETHING';
	},0);
	return this.result;
};
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы