Click here to Skip to main content
15,885,309 members
Home / Discussions / JavaScript
   

JavaScript

 
GeneralRe: Mutation Observer, editing a value Pin
Richard Deeming15-Aug-23 21:36
mveRichard Deeming15-Aug-23 21:36 
GeneralRe: Mutation Observer, editing a value Pin
jkirkerx16-Aug-23 6:33
professionaljkirkerx16-Aug-23 6:33 
AnswerRe: C++ Issue: Forgotten Virtual Destructor - Need Help Resolving! Pin
Richard MacCutchan15-May-23 1:53
mveRichard MacCutchan15-May-23 1:53 
GeneralRe: C++ Issue: Forgotten Virtual Destructor - Need Help Resolving! Pin
Richard Deeming15-May-23 5:25
mveRichard Deeming15-May-23 5:25 
Question[Learning/Beginner] Confused about await, async and fetch. Pin
Maximilien2-May-23 3:09
Maximilien2-May-23 3:09 
AnswerRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Jeremy Falcon2-May-23 5:07
professionalJeremy Falcon2-May-23 5:07 
GeneralRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Maximilien2-May-23 10:27
Maximilien2-May-23 10:27 
AnswerRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Richard Deeming2-May-23 22:33
mveRichard Deeming2-May-23 22:33 
Jeremy is correct - any function that uses await needs to be marked as async.

In this case, the callback to map would need to be async. However, that would result in an array of promises.

But since you're not doing anything with the array returned from either map call, that won't make much difference.

Assuming you actually want to do something with the results, try:
JavaScript
async function getPeople() {
    console.log("fetchNames");
    const url = "https://swapi.dev/api/people";
    const res = await fetch(url);
    const data = await res.json();
    console.log(data);
    
    const results = data.results;
    
    const promises = results.map(async (person) => {
        const filmPromises = person.films.map(getFilmName);
        const films = await Promise.all(filmPromises);
        return { person, films };
    });
    
    return await Promise.all(promises);
}

(async () => {
    const people = await getPeople();
    people.forEach(person => {
        console.log(`${person.person.name} stars in :`);
        person.films.forEach(filmName => console.log(filmName));
    });
})();
Demo[^]
Promise.all() - JavaScript | MDN[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Maximilien3-May-23 2:41
Maximilien3-May-23 2:41 
GeneralRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Jeremy Falcon3-May-23 10:13
professionalJeremy Falcon3-May-23 10:13 
GeneralRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Maximilien3-May-23 12:56
Maximilien3-May-23 12:56 
GeneralRe: [Learning/Beginner] Confused about await, async and fetch. Pin
Jeremy Falcon4-May-23 4:47
professionalJeremy Falcon4-May-23 4:47 
QuestionAxios Pin
darlina1-May-23 22:47
darlina1-May-23 22:47 
Questionjson and json-server with weird be valid json file. Pin
Maximilien21-Apr-23 2:17
Maximilien21-Apr-23 2:17 
AnswerRe: json and json-server with weird be valid json file. Pin
Richard Deeming21-Apr-23 2:32
mveRichard Deeming21-Apr-23 2:32 
GeneralRe: json and json-server with weird be valid json file. Pin
Maximilien21-Apr-23 4:15
Maximilien21-Apr-23 4:15 
GeneralRe: json and json-server with weird be valid json file. Pin
Richard Deeming21-Apr-23 5:37
mveRichard Deeming21-Apr-23 5:37 
QuestionApplication is "dead" when I open it in a browser Pin
arnold_w16-Mar-23 23:23
arnold_w16-Mar-23 23:23 
AnswerRe: Application is "dead" when I open it in a browser Pin
jschell5-Apr-23 6:30
jschell5-Apr-23 6:30 
QuestionJavascript Form Validation - Help needed Pin
Kobraz11-Mar-23 12:43
Kobraz11-Mar-23 12:43 
QuestionJavaScript Pin
Member 159403612-Mar-23 17:42
Member 159403612-Mar-23 17:42 
RantRe: JavaScript Pin
Richard Deeming2-Mar-23 21:24
mveRichard Deeming2-Mar-23 21:24 
JokeRe: JavaScript Pin
Jeremy Falcon8-Mar-23 12:57
professionalJeremy Falcon8-Mar-23 12:57 
SuggestionJavaScript Pin
Member 159403612-Mar-23 17:40
Member 159403612-Mar-23 17:40 
RantRe: JavaScript Pin
Richard Deeming2-Mar-23 21:24
mveRichard Deeming2-Mar-23 21:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.