I am trying to figure out this for a long time now.
I am trying to learn about how promises work and found this code in the article How do Promises Work? by Quil.
I can see how sidePromise’s dependencies are gonna be executed, its dependency being squareAreaAbstraction()
But how does squareAreaPromise
execute its dependencies?
I mean, we are calling the fulfil(sidePromise,10)
explicitly, but where does fulfil()
function for squareAreaPromise
get called to fulfil its dependencies?
var squareAreaAbstraction = function(side) {
var result = createPromise();
fulfil(result, side * side);
return result;
};
var printAbstraction = function(squareArea) {
var result = createPromise();
fulfil(result, console.log(squareArea));
return result;
}
var sidePromise = createPromise();
var squareAreaPromise = depend(sidePromise, squareAreaAbstraction);
var printPromise = depend(squareAreaPromise, printAbstraction);
fulfil(sidePromise, 10);
function createPromise() {
return {
// A promise starts containing no value,
value: null,
// with a "pending" state, so it can be fulfilled later,
state: "pending",
// and it has no dependencies yet.
dependencies: []
};
}
function depend(promise, expression) {
// We need to return a promise that will contain the value of
// the expression, when we're able to compute the expression
var result = createPromise();
// If we can't execute the expression yet, put it in the list of
// dependencies for the future value
if (promise.state === "pending") {
promise.dependencies.push(function(value) {
// We're interested in the eventual value of the expression
// so we can put that value in our result promise.
depend(expression(value), function(newValue) {
fulfil(result, newValue);
// We return an empty promise because `depend` requires one promise
return createPromise();
})
});
// Otherwise just execute the expression, we've got the value
// to plug in ready!
} else {
depend(expression(promise.value), function(newValue) {
fulfil(result, newValue);
// We return an empty promise because `depend` requires one promise
return createPromise();
})
}
return result;
}
function fulfil(promise, value) {
if (promise.state !== "pending") {
throw new Error("Trying to fulfil an already fulfilled promise!");
} else {
promise.state = "fulfilled";
promise.value = value;
// Dependencies may add other dependencies to this promise, so we
// need to clean up the dependency list and copy it so our
// iteration isn't affected by that.
var dependencies = promise.dependencies;
promise.dependencies = [];
dependencies.forEach(function(expression) {
expression(value);
});
}
}
I have tried running and debugging in VS code for over a day but I get lost in the depend() function
2
Answers
This is due to this part of the code:
When you do
expression
is the functionprintAbstraction
. So it adds a dependency that will callprintAbstraction(value)
. WHen you fulfillsquareAreaPromise()
it executes all its dependencies, and this calls that function.No, it’s not. Look at what exactly
depend
does put in thedependencies
array:Here,
expression
issquareAreaAbstraction
andresult
is thesquareAreaPromise
. So this is callingsquareAreaAbstraction
withsidePromise
‘s value (squareAreaAbstraction(10)
), and since that returns another promise, we calldepend
again to finallyfulfill(squareAreaPromise, newValue)
when that promise fulfills withnewValue
(which will be100
).