Good morning!
I have the following Angular/TypeScript service, but the query portion of the function happens asynchronously so the return statement is executed before the data is retrieved from the server. Is there a way to add async/await to this function so it waits for the data to be assigned to the Users variable before executing the return statement?
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);