Although I am familiar with async/await/then, I recently stumbled upon something new that sparked my curiosity:
Consider the following function:
HelloWorld():Promise<string>
{
return new Promise(resolve => {
setTimeout(() => {
alert("IN");
resolve('Hello World!');
}, 2000);
});
}
Now, if I call this function from another function like this:
async onClick()
{
var p:Promise<string> = this.HelloWorld();
await(p);
alert(p);
}
I will see alerts in the order below (after a 2-second delay):
'IN'
'[object Promise]'
My question is, by the time the second alert is triggered, the promise has already been resolved (due to the await(p) call), so its value is set ('Hello World!').
Is there any way to access this value without resorting to the typical .then approach?
For example:
var p:Promise<string> = this.HelloWorld();
await(p);
alert(<VALUE OF PROMISE HERE (which now equals 'Hello World!')>);
As I mentioned earlier, it's simply an experiment and some food for thought.