2
exports.img = function(){
  fetch("http://aws.random.cat/meow")
  .then(res => res.json())
  .then(json => console.log(json.file));
}

I have been trying to return the "file" value from the json i fetched but I keep either producing "promise { }" or "undefined". I'm able to log the info I want in the console but I haven't been able to return the value. I've been trying for hours now and need some direction. Also Ive already looked through these and some others.

This question is mainly to help me with node-fetch but I am in the process of working with modules as well.

Edit.

const fetch = require('node-fetch');

exports.img = async function cat(){
  var data = await fetch('http://aws.random.cat/meow');
  var json = await data.json();
  return json.file;
}

From the answers I was given I produced this code but still received a pending promise.

  • Follow this answer https://stackoverflow.com/questions/43320521/how-to-return-return-from-a-promise-callback-with-fetch#answer-43320592 – Masum Billah Mar 12 '19 at 07:59

0 Answers0