2

I was reading this article and trying to comprehend this code

function getRandomNumber(start = 1, end = 10) {
  //works when both start and end are >=1
  return (parseInt(Math.random() * end) % (end - start + 1)) + start;
}
var promiseTRRARNOSG = (promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator = function() {
  return new Promise(function(resolve, reject) {
    let randomNumberOfSeconds = getRandomNumber(2, 10);
    setTimeout(function() {
      let randomiseResolving = getRandomNumber(1, 10);
      if (randomiseResolving > 5) {
        resolve({
          randomNumberOfSeconds: randomNumberOfSeconds,
          randomiseResolving: randomiseResolving
        });
      } else {
        reject({
          randomNumberOfSeconds: randomNumberOfSeconds,
          randomiseResolving: randomiseResolving
        });
      }
    }, randomNumberOfSeconds * 1000);
  });
});
var testProimse = promiseTRRARNOSG();
testProimse.then(function(value) {
  console.log("Value when promise is resolved : ", value);
});
testProimse.catch(function(reason) {
  console.log("Reason when promise is rejected : ", reason);
});
// Let us loop through and create ten different promises using the function to see some variation. Some will be resolved and some will be rejected. 
for (i=1; i<=10; i++) {
  let promise = promiseTRRARNOSG();
  promise.then(function(value) {
    console.log("Value when promise is resolved : ", value);
  });
  promise.catch(function(reason) {
    console.log("Reason when promise is rejected : ", reason);
  });
}

Here author have done something like this var promiseTRRARNOSG = (promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator = function() { which I am unable to understand.

Would someone help me understand this line? I mean specifically that line of code/declaration.

halfer
  • 18,701
  • 13
  • 79
  • 158
iRohitBhatia
  • 3,808
  • 6
  • 38
  • 88

4 Answers4

3

Both promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator and promiseTRRARNOSG refer to the same function. Consider the following statement:

var a = (b = function() {});

Here a and b refer to the same function. The parens here do not do anything special. You can rewrite the statement this way:

var a = b = function() {};

The a here is defined as a regular variable and b is a global variable. That useless and long variable should have been a comment.

undefined
  • 136,817
  • 15
  • 158
  • 186
2

I see no reason for the promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator variable. It is not used.

It is functionally the same as

function promiseTRRARNOSG() { // same function body after here.
Adrian Brand
  • 15,308
  • 3
  • 24
  • 46
  • 1
    Noting the difference between [*function declarations and expressions*](https://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname) though. ;-) – RobG Sep 28 '18 at 00:11
2

Think about anonymous function which look like:

var promiseTRRARNOSG = function() {

but author gave a name to that function for "debug purposes" to get method name which was called in backtrace or maybe he/she tried to make an alias from long method name:

var promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator = function() { 
  return new Promise ...
}

var promiseTRRARNOSG = promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator;

but then changed his/her mind and made it like:

var promiseTRRARNOSG = (
    promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator = function() { 
        return new Promise ...
    }
);

As You might see in that post such line:

Let us call for our function promiseTRRARNOSG which is an alias for promiseThatResolvesRandomlyAfterRandomNumnberOfSecondsGenerator.

num8er
  • 15,883
  • 2
  • 33
  • 48
1

What the author has done is placed an anonymous function inside a variable, and that function has a return variable. So the function will return a value that will become the value of the variable the function is declared inside. So if I have a variable:

var test = function(text) {
    return text + " " + text;
}

Then making some more text like this:

var words = test("words");

Then words will have a value of "words words". This is a concept common in JavaScript frameworks such as jQuery.

Jack Bashford
  • 38,499
  • 10
  • 36
  • 67