DEV Community

Discussion on: Three Ways to Retrieve JSON from the Web using Node.js

Collapse
 
three_ninjas profile image
Voted Jason Of The Year 2003-2007

Hi Isa, this is very helpful! I have a really dumb question...feel free to ignore me, I'm new to node and promises and kinda new to Javascript.

In the node-fetch example, what if what I wanted to do with the json was return it?

Like:
const getStuffFromWhatever = (item) => {
let url = whatever?thing=item

let settings = { method: "Get" };

fetch(url, settings)
.then(res => res.json())
.then((json) => {
    return json
});
Enter fullscreen mode Exit fullscreen mode

}

console.log(getStuffFromWhatever('puppy dogs')

I have tried a few things that seem reasonable to me, but they don't work. I could move the fetch into the function I actually need it in, but I will need it again elsewhere and that's not very DRY.

Collapse
 
three_ninjas profile image
Voted Jason Of The Year 2003-2007

Every time I put myself out there and ask a question I am embarrassed to ask, I always find the answer 5 minutes later, and this time is no different. The answer is async / await! Of course it is.

Thanks for letting me talk it out.

Collapse
 
isalevine profile image
Isa Levine

Always happy to be your rubber ducky! :)