Alternative for Calling Async API for Each Item Inside array.map() in React (as async Can’t be Used inside array.map)?

Making a long story short, I would like to iterate in an array by using the array.map() function, and for each item in the array call an async API (from inside the array.map), but I know that this is not possible. Based on the scenario below, can you share your pro ideas and miraculous/magical solutions for this case with me?

SCENARIO:

  • I have images stored in a private S3 bucket in AWS, and my React application fetches the names of some of the images from a database and store them in an array. I need to display in the screen all the images in the array

  • As the images are in the private S3 bucket, I cannot simply use their paths (bucket name + the image name) as the source of the image element in the page (<img src=’aws.my-bucket.com.blablabla/image.jpg’ />). Instead, as per AWS guidelines, I have to generate a “pre-signed URL” for each image name, and this pre-signed URL is the one that is used for rendering the images (the API that generates this URL is ready, and If I get the URL generated by my API and paste it in the browser, I successfully see the image)

  • The tricky part is: how to loop into the array and call the API for generating the URL for each item based on the images names, if the API call has to be asynchronous, and it is not possible to call async functions from array.map()?

I thought of somehow doing it inside useEffect(), but couldn’t really visualize how to do so.

This is my current code, with the S3 path as the “src” of the <img/> element that is working because I made the S3 public. But I need to make it private and pass the pre-signed URL instead:

export default function Gallery() {

    {/*
    
    Some code that fetches the names of the images and stores them in the  array
    
    */}
    
    return (
        <div>
            {
                gallery.map(image=>
                    (
                        <figure>
                            <div>
                                <img src={`https://s3-location-amazonaws.com/my-bucket-name/${image.FILE_NAME}`} />  // <--- Here I need to somehow use the URL returned by the app.get('/s3/:bucket/:key' , async (req, res) API described below
                            </div>
                            <figcaption>{image.LABEL}</figcaption>
                        </figure>
                    )
                )
            } 
        </div>
        )
}

API in the back-end (node.js) that generates the URL needed for rendering the image:

app.get('/s3/:bucket/:key' , async (req, res) => {

  try {

    const clientUrl = await generatePreSignedUrl({
      bucket: `${req.params.bucket}`,
      key: `${req.params.key}`,
    });

    res.send(clientUrl);

  } catch (err) {
    console.error(err);
  }

})

Function that generates the pre-signed URL in case you want to take a look at it:

async function generatePreSignedUrl({ bucket, key }) {

  const { GetObjectCommand, S3Client } = require("@aws-sdk/client-s3");
  const { getSignedUrl, S3RequestPresigner }  = require("@aws-sdk/s3-request-presigner");

  // No need to pass the object with the region and the credentials to the S3 as this parameters were configured in AWS CLI by running the AWS CONFIGURE command in both the EC2 instance and the dev environment
  const client = new S3Client();
  const command = new GetObjectCommand({ Bucket: bucket, Key: key });
  return getSignedUrl(client, command);
}

I tried to call the API asynchronously from inside the array.map() but of course it didn’t work

New contributor

GenerikError is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật