Currently, I am delving into the world of Angular 2, typescript, promises, and more. I've set up a small application for developer tools with a service that simply returns hard-coded data. This setup is purely for testing purposes.
I want to introduce a short delay in the service method to mimic server lag and test some of my controls effectively. However, I am struggling to find the correct syntax to achieve this. How can I insert a 5-second delay in my service call?
Developer Tools Service
@Injectable()
export class DeveloperService {
getExampleData(): Promise<ExampleItem[]> {
const examples: ExampleItem[] = [];
examples.push({ id: 1, name: 'Spaceman Spiff', location: 'Outer Space', age: 12 });
examples.push({ id: 2, name: 'Stupendous Man', location: 'The City', age: 30.5 });
examples.push({ id: 3, name: 'Tracer Bullet', location: 'The City', age: 24 });
examples.push({ id: 4, name: 'Napalm Man', location: 'War Zone', age: 43.333 });
examples.push({ id: 5, name: 'Adult Calvin', location: 'In the future', age: 54 });
// TODO: Add a delay here!
return Promise.resolve(examples);
}
}
Developer Tools App
getExampleData() {
return (): Promise<Array<any>> => {
return this.developerService.getExampleData();
};
}
UPDATE: 1 I attempted to use setTimeout() within the control implementation call but faced issues with getting the data populated at that specific time. It would be ideal to incorporate the delay directly into the service call method to avoid having to add it repeatedly.
getExampleData() {
setTimeout(() => (): Promise<Array<any>> => {
return this.developerService.getExampleData();
}, 5000);
}