Back to blogs
Blog
Writing in public
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Blog
Longer posts — published here first, then cross-posted to Dev.to with a canonical URL back to this site.
Back to blogs
Back to blogs
Postjavascript, promises, loops, api
The code i was working on Live preview While using GitHub's rest API i found out that fetching the...
Date published

Why You Might Still Pick Next.js Over TanStack Start From a TanStack Start...

A complete theme management system for React applications with SSR support! ✨ 🌟...

A guide to creating Android home screen widgets using Expo modules, complete with state management,...
While using GitHub's rest API i found out that fetching the user's followers doesn't return if you follow the user and you have to make a separate request for that .
So far so good, so i added a forEach loop that awaits for the second request then inserts whether the user is following me
1export const getUserWithFollowingDetails=async(token:string,url:string,username:string)=>{2 let followers:any=[]3const users = await getAuthedUserFollowers(token,url)4users.forEach((user)=>{5user.following_me = await getIsUserFollowingMe(token,username,user.login)6}).catch((e)=>{})7followers.push(user)8 })9 return followers10}
The code worked , but it had weird outcomes. The code eventually looked the way i expected but it would be too late for react to notice it and re-render so the data being output was not matching the values being console logged , after some googling I came upon . for async.. of which is a way of looping over items and do await while async actions happen to the array objects.
1export const getUserWithFollowingDetails=async(token:string,url:string,username:string)=>{2 let followers:any=[]3 const users = await getAuthedUserFollowers(token,url)4 for await (const user of users){5 //@ts-ignore6user.following_me = await getIsUserFollowingMe(token,username,user.login)7 .catch((e)=>{})8 followers.push(user)9 }10 return followers11 }12