Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Can't get promise to resolve

The bane of my existence is getting promises to resolve. Here is the latest example

const axios = require('axios');
const keys  = require('../config/keys');

const weatherURI = `${keys.darkskyBaseURL}/${keys.darkskyAPIKey}/${keys.lat},${keys.lng}`;

const weather = async (req, res) => {
    try {
        res = await axios.get(weatherURI);
        return res.data.json();
        //  data returned
        //      res.data.currently{}
        //      res.data.daily.data[{}]
        //      res.data.alerts[{}] (conditional)
    }
    catch(error) {
        console.log(error);
    }
}

const forecast = weather();

module.exports = forecast;

I am simply trying to get forecast to resolve to the JSON Object I know it is bringing back. I cannot get past the Promise {pending} state.

Brendan:

I will take a look at the courses you recommend. I have to get a handle on this stuff.

There are also some tutorials out there like this one to build a promise from scratch. It can help to understand what's going on with promises rather than just being a strange abstract object.

1 Answer

There are two new courses out today, Rest APIs with Express and Asynchronous Code in Express that will likely help you groc this stuff. The old treehouses on Express were not promise-based.

I also recommend Understanding Promises in JavaScript and Working with the Fetch API if you haven't already (fetch is similar to axios and is now native to modern browsers. You can use a library like node-fetch if you want to use fetch on the node side).