What is the standard built-in JavaScript function to create an abstract promise? By abstract, I mean that I can attach dependencies before or after the resolution method is defined.
For instance, I'd like to be able to just write:
// page load sequence
let Cli_Cfg = New_Promise(); // client configuration loaded
let Initial_View = New_Promise(); // complete view is displayed
Cli_Cfg.then((cfg)=>console.log(cfg)); // does not print
Cli_Cfg.resolve("client config"); // prints "client config"
Cli_Cfg.then(()=>console.log("MORE")); // prints "MORE"
The problem with simply using new Promise(f)
is that, case by case, I have to write a function f
that immediately starts doing work towards resolving the promise. But in my example above, the means to actually load the client configuration is not even defined until 15 other things are fetched, initialized, cross-consulted, compiled, and baked.
As a hack, I came up with this:
function New_Promise()
{
let resolve;
let result = new Promise(r => resolve = r);
result.resolve = resolve;
return result;
}
What is the built-in function that does this? (Or, if you think none exists, please give a reason why the language standard committees might have rejected this obvious idea.)