I mean
opA
is a function, so why notopA()
?
Because opA
is a reference to the function itself. The promise will use that reference to execute that function at a later time.
Alternatively, opA()
executes the function (without any arguments) now and passes the result of that function call to the promise. Since your opA
function doesn’t return anything, it would pass undefined
to the promise. The promise would then have nothing to execute later after it completes its operation(s). The setTimeout
would then also fail because cb
is also undefined
, because no arguments were passed to opA()
.
Any time you do see a structure like that, either (1) it’s a bug or (2) the function intentionally builds and returns a callback function intended for the promise. For example, if you write a function which returns opA
then you can invoke that function and pass its result to promisify
.
An important clue to this behavior is here:
const opA = ...
opA
is a variable, not unlike any other variable. It contains a value or a reference to something. In this case it’s a reference to a function. You could re-assign that variable, pass it as a function argument, set it as a property on an object, etc. just like any other.
solved Why doesn’t promisify want the argument for the callback function?