0
import "@babel/polyfill";

export async function resolveRemotePost(url){
        const fetch = require('node-fetch');

        return await fetch(url).then((data) => data.json());
}

export function getRemoteContent(url){
        let webpage = resolveRemotePost(url);
        console.log("Remote content")
        console.log(webpage);

The following code tries to call a url and assign the json data to the variable webpage. However what it does instead is return a "promise". (I'm new to javascript). I understand how asynchronous functions work (i think) but in javascript I cannot seem to figure out how to exactly get the data I'm trying to fetch. My question is how do I unwrap this promise to retrieve the data contained within it?

  • Does this answer your question? [Return from a promise then()](https://stackoverflow.com/questions/34094806/return-from-a-promise-then) – Chase Oct 28 '20 at 06:43
  • 2
    why are you using `.then` and `await` in the same line? Please consider reading through how [promises](https://javascript.info/promise-chaining) and [`async/await`](https://javascript.info/async-await) works in javascript. – Chase Oct 28 '20 at 06:44
  • @Chase If I'm understanding the post you've linked to, then consumes a promise and any function called on the data within it operates on the actual data and not the promise. Is that correct? – patriots784544 Oct 28 '20 at 06:47
  • `.then` itself returns a promise than can be chained with another `.then` to get the value. But the first major issue here is that you're using both `await` and `.then` at the same time - most likely since you're confused about how `await` and `.then` work. You should need only one of them – Chase Oct 28 '20 at 06:49
  • `async` functions return a Promise ... that's all they return, ever - so if you make `getRemoteContent` `async` then you can `let webpage = await resolveRemotePost(url);` - and you can remove `async` from `resolveRemotePost` and `return fetch(url).then((data) => data.json());` – Jaromanda X Oct 28 '20 at 06:54

0 Answers0