When it comes to subscription blocks, extracting the value from my API is possible.
var data = null;
this._timinServiceProxy.getDateFromNTP().subscribe(result => {
data = result;
console.log(data); // The expected result
});
console.log(data); // It will currently be null.
I understand that this is the expected behavior due to the asynchronous nature of the operation, but I still need to access the result somehow.
.subscribe(result =>
Trying something like using toPromise() yields:
const date = this._timinServiceProxy.getDateFromNTP().toPromise();
return date;
This returns a ZoneAwarePromise
with a property __zone_symbol__value
, which holds the correct DateTime object from the API. How can I retrieve this value? I must find a way to access either __zone_symbol__value or the subscribed result from outside the scope.
getDate(): DateTime {
if (abp.clock.provider.supportsMultipleTimezone)
{
return DateTime.local().setZone(abp.timing.timeZoneInfo.iana.timeZoneId);
}
else
{
//return DateTime.local();
// RETURN getDateFromNTP()
}
}
Additionally, I am faced with the challenge of returning a DateTime while my service returns either a Promise or an Observable.
UPDATE
You can view my problem on Stackblitz.
The issue lies in the fact that although I can obtain DateTime using toPromise, async methods return a Promise which is not ideal as I only require the DateTime. Despite trying various solutions, such as setting the global property after calling the async method and then fetching it for subsequent use, I ultimately had to resort to setting DateTime as static. You can refer to date-pipe.component.ts
for more details.