JavaScript awaited Promises still returning <pending>

I have an array of data objects about people. Each person object includes 0-n URLs for additional info (guests of the person).

I want to process this list, calling each of the 'guest' URLs and including the guest's names in the original data set.

Context: this is an AWS lambda function. I'm using lambda-local to run this locally. (lambda-local -l index.js -e fixtures/test_event1.json).

I'm successfully using await/async to retrieve the initial set of data.

But I'm unable to get if working for these further calls about guest info. It always shows a pending Promise, even though the result is awaited.

// index.js

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

exports.handler = async function(event){

    try {
        let registrations = await getEventRegistrations();
        console.log(registrations);

/* All good here - sample console output
[ { contactId: 43452967,
    displayName: 'aaa, bbb',
    numGuests: 0,
    guestUrls: [] },
  { contactId: 43766365,
    displayName: 'bbb, ccc',
    numGuests: 1,
    guestUrls:
     [ 'https://<URL>' ] },
  { contactId: 43766359,
    displayName: 'ccc, ddd',
    numGuests: 2,
    guestUrls:
     [ 'https://<URL>',
       'https://<URL> ] } ]
*/

        // Expanding the guest URLs is proving problematic - see expandGuests function below
        registrations = registrations.map(expandGuests);
        console.log(registrations);


/* Registrations are just pending Promises here, not the real data

[ Promise { <pending> },
  Promise { <pending> },
  Promise { <pending> } ]

*/

        return {
            statusCode: 200,
            headers: {
                'Access-Control-Allow-Origin': '*',
            },
            body: JSON.stringify(registrations),
        };
    }
    catch (exception) {
        console.log(exception);
        return {
            statusCode: 500,
            body: 'Unable to retrieve data.'
        };
    }
};

function getEventRegistrations() {
    return fetch('<URL>')
    .then(res => res.json())
    .catch(function (error) {
        console.log('Event registrants request failed', error);
        return null;
    });
}

function getGuestName(url) {
    return fetch(url)
    .then(res => res.json())
    .then(guest => guest.DisplayName)
    .catch(function (error) {
        console.log('Guest name request failed', error);
        return null;
    });
}

async function expandGuests(data) {

    const promises = data.guestUrls.map(url => getGuestName(url));
    data.guestNames = await Promise.all(promises);

    return data;
}


How can I resolve these pending Promises and thus return useful data?

Thank you.

Answer:1

The comments are correct in pointing out that mapping an async function will return an array of Promises. What they don't explicitly mention is that you have two maps, and which one is problematic.

The issue is in the line:

reservations = reservations.map(expandGuests);

Any time you use map, you'll need to actually resolve the promises that return.

So:

const mappedPromises = reservations.map(expandGuests);  //returns an Array of Pending promises
const mappedReservations = await Promise.all(mappedPromises);  //resolves the promises
Answer:2

Performing For..Loops or While..Loops does not update the $scope variable. I'm trying to show the progress as loops are incremented. I have read (http://jimhoskins.com/2012/12/17/angularjs-and-apply....

Performing For..Loops or While..Loops does not update the $scope variable. I'm trying to show the progress as loops are incremented. I have read (http://jimhoskins.com/2012/12/17/angularjs-and-apply....

  1. angularjs loop not updating

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I'm studying rxjs Observables and I couldn't understand why the setInterval inside Observable.create is still running even I have unsubscribe to that observable object. Observable is async right? When ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...

I have a simple set up to find top scores. The goal is to order who received the highest scores and then give the top 3 bonus points. Question: In part two below I am looping through the people and ...

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...

I have basically this structure for my data (this.terms): { name: 'First Category', posts: [ { name: 'Jim James', tags: [ 'nice', 'friendly' ...