Hey there!
I'm working with this Angular/TypeScript service, and I've run into an issue where the query part of the function is asynchronous. This means that the return statement gets executed before the data is actually retrieved from the server. Is there a way to modify this function to use async/await so it waits for the data to be assigned to the Users variable before returning?
interface IUserService {
getAll(): entities.IUser[];
}
export class UserService implements IUserService {
static $inject: string[] = ['dataAccessService'];
constructor(private dataAccessService: dataAccess.DataAccessService<entities.IUser>) {
}
getAll(): app.domain.entities.IUser[] {
var users: app.domain.entities.IUser[];
var userResource = this.dataAccessService.getDataResource('https://api.github.com/users');
userResource.query((data: app.domain.entities.IUser[]) => {
users = data;
});
return users;
}
}
angular
.module('domain')
.service('userService',
UserService);