To get a standard API for this, you’ll need to convince the JavaScript standard committee, as well as the browser vendors, that your use case is widespread and important enough to be worth the standardization and implementation burden, and that it cannot be achieved in any other possible way.
I am maintaining an existing API that includes asynchronous functions (mix
of callbacks and Promises) and synchronous functions. After some
asynchronous initialisation, the internal state settles and it is perfectly
safe to use the synchronous functions as expected.
So, I’d like to emit warnings when these synchronous functions are called
prior to a Promise being “settled”. That way, downstream developers will
know that they should be waiting for the Promise to settle before using
such functions.
This actually isn’t too different to the XHR / Fetch APIs conceptually. We
get the ball rolling with an asynchronous API call, but there are
deterministic blocks within which we can synchronously interrogate
progress, etc.
activity indicator use case
I use a Promise to represent a network transaction. I wish to alter the
visual state of my web app to reflect the state of this network
transaction. I can, for example, show an indeterminate progress bar whilst
the Promise is not “settled”.
If I am using requestAnimationFrame, or a framework like React, then the
state would be synchronously mapped to the DOM / canvas during each
execution of my render function.
I can track the state of the Promise using additional variables (as others
have suggested), but those state values already exist somewhere private per
the functioning of a Promise. I’d be duplicating work that the JavaScript
engine is already performing internally, at the risk of introducing errors
in my code.
third-party popular libraries
The following libraries implement some form of Promise and all expose such
synchronous inspection capabilities:
Results can be a bit variable because deferring is unpredictable, but the following are reasonably representative on my (otherwise idling) desktop:
Standard promises: Chrome: 1200ms, Firefox: 600ms
Promises with sync inspection: Chrome: 150ms, Firefox: 200ms
The promises with sync inspection would be even faster but the code honors a 12ms task limit. Probably a large part of the difference is that with standard promises reduce() requires the creation of an additional promise every time the callback returns a promise.
Hi! I curious what do you mean when you tell synchronously and
Then will not tell us the actual state of the promise. But it will wait until the promise will be resolved.
And if by synchronously you just mean to get the actual status with no waiting till the Promise will be resolved (which actually may even never happen) there’s an utility promise-status-async which is not a sync but does such a trick
const {promiseStatus} = require('promise-status-async');
const myPromise = new Promise(resolve => setTimeout(resolve, 10000));
console.log('promise status is "%s"', await promiseStatus(myPromise));
// -> promise status is "pending"
await myPromise;
console.log('after resolution status is "%s"', await promiseStatus(myPromise));
// -> promise status is "resolved"