Javascript: passing data from local to global

Viewed 42

I am practicing AWS-S3 bucket and passing data from local to global scope. It's an simple Javascript concept. I have array of strings value which I am getting from S3 bucket. I can see my data when I console it. I am trying pass this data to global scope. I tried push method, spread operator but it did not work. Global scope gives me empty array. I don't get what I am doing wrong.

I get data from S3 bucket like this

["182363", "j9292092", "20282"]

I want pass my data to global variable exactly this.

this is my piece of code

  const allSKU = []
  await s3.getObject(params, function (err, data) {
    if (err) console.log(err, err.stack);
    // else console.log(data.Body.toString('utf-8')); // I can see the data
    const allSku = data.Body.toString('utf-8')
    //console.log(allSku);
    allSKU.push(allSku)
  });


  console.log(allSKU); // give me empty arrays
1 Answers

when passing callback method, await has no effect as method is not returning a promise

s3.getObject(params, function (err, data) => {})

we can return a promise, by calling .promise() on most of the AWS v2 SDK methods.

   const data = await s3.getObject(params).promise()
Related