Hi i have a function which should return Array, in the below function, this.cordovaFile.readAsArrayBuffer(this.cordovaFile.dataDirectory, storageId) actually returns an Promise Array which i am converting it into Observable and storing into timesheetArray variable and Now timesheetArray it will return Observable array but i just want to return just a Array. Below is the code
Please help, If it return just an array i dont need to change it anywhere , because this function is used through out the application
public getAllTimesheets(): TimesheetModel[] {
const storageId = TIMESHEET_KEYS.ALL_TIMESHEET;
const timesheetArray = from(
this.cordovaFile
.readAsArrayBuffer(this.cordovaFile.dataDirectory, storageId)
.then((compressedTimesheet) => {
const start = moment();
const uint8array = new Uint8Array(compressedTimesheet);
const jsonTimeSheet = this.LZString.decompressFromUint8Array(uint8array);
this.log.debug(`LocalStorageMaterialService: getMaterials() from files: Decompression took ${moment().subtract(start.valueOf()).valueOf()} ms`);
return <TimesheetModel[] > JSON.parse(jsonTimeSheet) || [];
})
.catch((error) => {
this.log.debug('LocalStorageMaterialService: Retrieving materials from file storage was not possible: ', JSON.stringify(error));
return [];
})
)
timesheetArray.subscribe((timesheet) => {
// here how to return an Array ??
});
}
and just one example why i want to return an array but not observable
let matchedTimesheet = _.find<TimesheetModel>(this.getAllTimesheets() ,
(timesheet) => travelToDate
&& timesheet.startOfWork.isSame(travelToDate.startDate.value, 'days')
); ```
here in the above code it is expecting an array but not Observable., I can do it by subscribing here also , but if the function returns an array instead of observable, then i need to change everywhere
getAllTimesheets()effectively updates the service copy. Or better call next on a BehaviorSubject that clients can subscribe to.const subscription = getAllTimesheets().subscribe(timesheetData => { // Here you'll get that data. let matchedTimesheet = _.find<TimesheetModel>(timesheetData, timesheet) => travelToDate && timesheet.startOfWork.isSame(travelToDate.startDate.value, 'days')); })