What is the best way to pass variables into a Promise?
I am in need of a versatile Promise that can be utilized by multiple functions. The Promise is responsible for sending an ajax request to determine the number of entries in a mySQL table and then setting a global variable (let's call it numberOfEntries = #).
The ajax request requires two specific variables to function properly.
var promise = new Promise(function(resolve, reject) {
var request = $.ajax({
method: "POST",
url: "grabDataFromSQL.php",
data: { data1: variable1, data2: variable2 }
});
request.done(resolve(data));
Therefore, a function using the promise (ultimately accessing the global variable) would follow this structure:
function doSomeStuff() {
promise.then( // complete task here )
}
The promise always necessitates those 2 variables to operate effectively. So... what is the solution? Am I approaching this correctly or is there a flaw in my logic? I seem to be at an impasse.
One possible approach is defining the promise within the same function that calls promise.then() and passing the variables into that function. However, I am hesitant to duplicate all that code repeatedly when the outcome remains consistent each time.