const sampleUsers = [ 'AdeleV@tenantname.onmicrosoft.com', 'AlexW@tenantname.onmicrosoft.com', 'DiegoS@tenantname.onmicrosoft.com', 'IsaiahL@tenantname.onmicrosoft.com', 'LeeG@tenantname.onmicrosoft.com' ];
const getUserPhotos = async () => { let finalResponse: any[] = []; Promise.all(sampleUsers.map(async user => { try { let response = await props.graphClient.api(`/users/${user}/photos/240x240/$value`).responseType('blob').get(); finalResponse.push(response); } catch (error) { console.log(error); } })).then(() => { console.log(finalResponse); }); }
The method getUserPhotos iterates the sampleUsers and sends individual requests for each user. The entire requests are declared inside the Promise, which means that the final result should contain all the users' photos. Below is the output of the above code in the browser console.
const batchItemLimit: number = 18; const userBatchLimit: number = 6; const sampleUsers = [ 'AdeleV@tenantname.onmicrosoft.com', 'AlexW@tenantname.onmicrosoft.com', 'DiegoS@tenantname.onmicrosoft.com', 'IsaiahL@tenantname.onmicrosoft.com', 'LeeG@tenantname.onmicrosoft.com' ];
const getUserThumbnailPhotos = async (): Promise<any[]> => { let finalResponse: any[] = []; return new Promise(async (res, rej) => { if (sampleUsers && sampleUsers.length > 0) { let requests: any[] = []; if (sampleUsers.length > userBatchLimit) { let chunkUserArr: any[] = chunk(sampleUsers, userBatchLimit); Promise.all(chunkUserArr.map(async chnkdata => { requests = []; chnkdata.map((user: any) => { requests.push({ id: `${user}_1`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/48x48/$value` }, { id: `${user}_2`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/96x96/$value` }, { id: `${user}_3`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/240x240/$value` }); }); let photoReq: any = { requests: requests }; let graphRes: any = await props.graphClient.api('$batch').post(photoReq); finalResponse.push(graphRes); })).then(() => { res(finalResponse); }); } else { sampleUsers.map((user: any) => { requests.push({ id: `${user}_1`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/48x48/$value` }, { id: `${user}_2`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/96x96/$value` }, { id: `${user}_3`, method: 'GET', responseType: 'blob', headers: { "Content-Type": "image/jpeg" }, url: `/users/${user}/photos/240x240/$value` }); }); let photoReq: any = { requests: requests }; finalResponse.push(await props.graphClient.api('$batch').post(photoReq)); res(finalResponse); } } }); };
The code compared to the previous is huge, but the logic is simple.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)