0

I'm fairly new to JavaScript and AlaSQL, so sorry if this is obvious.

I'm trying to save the result of my AlaSQL query to a variable so I can use it elsewhere in my code. As a bit of context, I'm querying a CSV file, and I'm wanting to get the max, min and a certain row of a particular column, so I can then use this elsewhere. At the moment I'm just trying it on the max aspect.

<script>
    alasql.promise('VALUE OF SELECT MAX(PM1) FROM CSV("http://localhost:8000/assets/interpolated.csv", {separator:","})').then(function(res){console.log(res); document.getElementById("testingId5").innerHTML = res}).catch(function(err){console.log('Error:', err);});
</script>

This produces a result and logs it in the console, and shows it onscreen, but I want to set it to a variable to be able to use too!

I've tried just adding var test = res; within the .then(function(res){}) part, but with no luck.

Thanks in advance.

Tim R
  • 3
  • 1
  • 4
  • `test = alasql.promise('VA...` and later: `test.then(res=>...use res` a promise is a value that is available at a later time (or fails). So you can't just do `test=res` that's like setting a variable to the button that's clicked by a user before the user clicks the button. – HMR Mar 27 '18 at 12:54
  • Okay, that makes some sense. But is there any way of just getting the value out from the promise? I want to be able to use multiple values (max, min, etc.) for some formulas later, so could do with them being assigned to variables. Or is there a way of using AlaSQL without promises when using CSV files, so I can just use the values? – Tim R Mar 27 '18 at 13:34

0 Answers0