I have a unique issue that I haven't been able to find a solution for on StackOverflow:
Within an Angular 6 service, I am trying to call a function from another service using TypeScript. Here is the code snippet:
Service1:
myArray: Array<IMyInterface>
...
getArray(): Observable<IMyInterface[]> {
this.myArray= this.service2.getAnArray(1);
return Observable.of(this.myArray);
}
Service2
getAnArray(myEntityId): Array<IMyInterface> {
const myArray2: IMyInterface[] = [];
this.getConnection().then(connection => {
connection.invoke('GetEntityById', myEntityId).then((someJson: any) => {
someJson.forEach( x => myArray2.push(x));
return myArray2;
})
});
}
While executing this, I encounter an error in Service2 stating
A function whose declared type is neither 'void' nor 'any' must return a value.
The issue arises because I need to return the populated array myArray2
only after resolving
connection.invoke('GetEntityById', myId)
, which is why I am attempting to do it within the then
block.
How can I overcome this challenge?