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.
- https://www.npmjs.com/package/node-fetch#api
- Promise pending
- https://blogs.missouristate.edu/cio/2016/01/14/fetching-data-over-http-with-nodejs-using-node-fetch/
- https://medium.com/@tkssharma/writing-neat-asynchronous-node-js-code-with-promises-async-await-fa8d8b0bcd7c
- https://evie.gitbook.io/js/modules
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.