I am having some troubles implementing AngularJS $q to get data from an async function and use it in an outer scope. Basically, I want to make the last line work in the below code. I have seen some examples but can't seem to wrap my head around AngularJS $q implementation.
var app = angular.module("myShoppingList", []);
app.controller("myCtrl", function ($scope, $q) {
const Papa = require('papaparse'); //I know these won't work, just added to give context
const AWS = require('aws-sdk')
AWS.config.update({
//keys go here
})
const s3 = new AWS.S3()
/* actual parameters should go here */
const params = {
Bucket: "test-bucket-2019",
Key: "dummy.csv"
};
const parseOptions = {
header: true,
dynamicTyping: true /* will assume numbers are actually numbers, yada yada */
}
let deferred = this.$q.defer(); //how to use this???
function getS3Data() {
s3.getObject(params, function (err, data) {
if (err) console.log(err, err.stack);
else {
const csv = data.Body.toString('utf-8');
const headers = 'id,start,end,count';
const parsed = Papa.parse(headers + '\n' + csv, parseOptions);
var parsedData = parsed.data;
console.log(parsedData); //this works
}
return parsedData;
})
}
console.log(parsedData) //how to make this work
});