In the examples I see, the code in a promise is static. An example:
var promise = new Promise(function (resolve,reject) {
  if (true)
    resolve("It is a success!")
  else
    reject(("It is a failure."));
});
promise.then(function (x) {
   alert(x);
}).catch(function (err) {
      alert("Error: " + err);
    });
How do I pass arguments to the promise so that useful work can be done? Is it by the use of global variables?
Usually it may be done with the following code:
function getSomePromise(myVar) {
  var promise = new Promise(function (resolve,reject) {
    if (myVar)
      resolve("It is a success!")
    else
      reject(("It is a failure."));
  });
  return promise;
}
var variableToPass = true;
getSomePromise(variableToPass).then(function (x) {
  alert(x);
}).catch(function (err) {
  alert("Error: " + err);
});
Update:
As @AlonEitan suggested, you can simplify getSomePromise function:
function getSomePromise(myVar) {
  return new Promise(function (resolve,reject) {
    if (myVar)
      resolve("It is a success!")
    else
      reject(("It is a failure."));
  });
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With