I have to use (large amounts of) existing code in an Angular 2 environment. That code makes extensive use of the $timeout service from AngularJS 1.x. As opposed to various other AngularJS 1.x services that are used in the code, I am having a hard time finding a information about an Angular 2 equivalent for the $timeout service.
The Angular docs do not seem to contain any mention of a service with timeout-something in its name. The article Upgrading from AngularJS does mention the scenario I am facing:
Maybe you want access to AngularJS's built-in services like
$locationor$timeout.
Unfortunately, the article does not actually explain how to access those particular services, as the subsequent example HeroesService assumes a service without any dependencies supplied by AngularJS 1.x.
Articles such as this one suggest using the native setTimeout function does not live up to the capabilities of the $timeout services, either.
How can I reproduce the $timeout functionality in the Angular 2 environment?
EDIT: As has been noted in the answers, the drawbacks of the native setTimeout function are not relevant when using Angular 2. In that case, if I had the full $q from AngularJS 1.x, I could replicate the $timeout function roughly like this:
function $timeout(fn, delay) {
var result = $q.defer();
setTimeout(function () {
$q.when(fn()).then(function (v) {
result.resolve(v);
});
}, delay);
return result.promise;
}