@@ -8,43 +8,19 @@ function getParticipants() {
88 // Define the ID of the list element where the user information will be inserted
99 const LIST_ID = "ack_group" ;
1010
11- /**
12- * Fetches users and their affiliations from the API and returns an array of user information.
13- * Each element in the array is an object containing the user's title and affiliation.
14- *
15- * @async
16- * @function getUsersInfo
17- * @returns {Promise<Array<{title: string, affiliation: string}>> } - A promise that resolves to an array of user information.
18- */
1911 async function getUsersInfo ( ) {
20- // Send a GET request to the users endpoint
2112 const response = await fetch ( `${ BASE_URL } /users` ) ;
22- // Fetch the JSON data from the response
2313 const data = await response . json ( ) ;
24- // Extract the list of users
2514 const users = data . _links . users ;
26-
27- // Initialize an empty array to store user information
28- let usersInfo = [ ] ;
29- // Iterate over each user
30- for ( const user of users ) {
31- // Fetch the affiliation of the current user
15+
16+ const usersInfo = await Promise . all ( users . map ( async ( user ) => {
3217 const affiliation = await getAffiliation ( user ) ;
33- // Push an object containing the user's title and affiliation to the usersInfo array
34- usersInfo . push ( { title : user . title , affiliation : affiliation } ) ;
35- }
36- // Return the array containing information of all users
18+ return { title : user . title , affiliation : affiliation } ;
19+ } ) ) ;
20+
3721 return usersInfo ;
3822 }
39-
40- /**
41- * Fetches the affiliation of a given user from the API.
42- *
43- * @async
44- * @function getAffiliation
45- * @param {Object } user - The user object.
46- * @returns {Promise<string> } - A promise that resolves to the title of the user's affiliation.
47- */
23+
4824 async function getAffiliation ( user ) {
4925 // Send a GET request to the affiliations endpoint of the user
5026 const response = await fetch ( user . href + "/affiliations/" ) ;
@@ -58,23 +34,18 @@ function getParticipants() {
5834 // Return the title of the affiliation
5935 return affiliation ;
6036 }
61-
62- /**
63- * Fetches users and their affiliations, creates a list item for each user with their title and affiliation,
64- * and appends these list items to a specified list in the document.
65- *
66- * @async
67- * @function insertUsersInfoIntoDocument
68- */
6937 async function insertUsersInfoIntoDocument ( ) {
7038 const usersInfo = await getUsersInfo ( ) ;
7139 const usersList = document . querySelector ( `#${ LIST_ID } ul` ) ;
72-
73- for ( const user of usersInfo ) {
40+ const fragment = document . createDocumentFragment ( ) ;
41+
42+ usersInfo . forEach ( user => {
7443 const li = document . createElement ( "li" ) ;
7544 li . textContent = `${ user . title } (${ user . affiliation } )` ;
76- usersList . appendChild ( li ) ;
77- }
45+ fragment . appendChild ( li ) ;
46+ } ) ;
47+
48+ usersList . appendChild ( fragment ) ;
7849 }
7950
8051 insertUsersInfoIntoDocument ( ) ;
0 commit comments