-1

Sorry if the question doesn't make a lot of sense, because it's honestly going to be hard for me to explain, but the gist of it is that I have a list of images that I need to get from our AWS S3 bucket inside an async map function. After it has iterated through all of the images that I need, I need to return the results outside of the Promise.all() method that this map function is inside. Let me show you what I meant by the code I have.

let res = Promise.all(ads.map(async (ad) => {
            let result = await ListObjects('bucket', '/', `Items/${ad.ID}_${ad.Year}`, (response) => {
                if(!response){
                    console.error('There was an issue getting these images');
                }else if(response.Contents.length > 0){
                    let list = response.Contents.map(obj => ({Path: obj.Key, Date: new Date(obj.LastModified).getTime()}));
                    list.sort((a,b) => a.Date < b.Date ? 11 : a.Date > b.Date ? -1 : 0);
                    return list[0];
                }else{
                    console.log('no previous ads')
                }
            })

            console.log(result);  // I expect a single object from ListObjects but I get undefined

            return result
        }));

        console.log(res); // expecting to get an array of objects here, but I get undefined
        return res;
}
export const ListObjects = (bucket, delimeter, prefix, callback) => {
    try{
        // could just use the s3 client config instead.
        aws.config.update({
            accessKeyId: access_key,
            secretAccessKey: secret_access_key,
            region: 'myregion'
        });
    
        const s3 = new S3();
        s3.listObjectsV2({
            Bucket: bucket,
            Delimiter: delimeter,
            Prefix: prefix
        }, (err,data) => {
            if(err){
                console.error(err);
                callback(false);
            }else{
                callback(data);
            }
        })
    }catch(e){
        console.log('ERROR: S3.ListObjects: ' + e);
        callback(false);
    }
}

My ListObjects method uses a callback and I'm not sure if this would need to be a promise instead of a callback function to make this request possible, but any clue on how I can make this possible would really help. TIA!

Michael
  • 1,454
  • 3
  • 19
  • 45

1 Answers1

-1

Is this second code snipped "yours"? If so, you just don't return your callback function, so result is undefined.

Igor Nowosad
  • 529
  • 3
  • 9