I can't wrap my head around this.
Take the following three functions where a calls b, and b calls c:
const c = () => {
return new Promise( (resolve, reject) => {
setTimeout(() => {
resolve(new Date());
}, "1750");
});
};
const b = async () => {
const result = await c();
console.log("b(): %s", result);
return result;
};
const a = async () => {
const result = await b();
console.log("a(): %s",result);
return result;
};
console.log("Starting...");
const final_result = a();
console.log("Final Result: %s", final_result);
console.log("Ending...");
I expected b() to wait-for/get the result of the promise returned by c(), and pass the value to a(). However, it looks like the promise gets passed all the way up the call stack.
To get the behavior I want, I have to handle the promise in each function:
const c = () => {
return new Promise( (resolve, reject) => {
setTimeout(() => {
resolve(new Date());
}, "1750");
});
};
const b = async () => {
const result = await c();
console.log("b(): %s", result);
return result;
};
const a = async () => {
const result = await b();
console.log("a(): %s",result);
return result;
};
(async () => {
console.log("Starting...");
const final_result = await a();
console.log("Final Result: %s", final_result);
console.log("Ending...");
})()
Why can't I just get the result of the promise in one function and return that? And, if I can do it, how?

