I\'m currently trying to learn Angular 2, typescript, promises, etc. I\'ve setup a small app for developer tools and a service that just returns hard-coded data. This is to
undefined
return new Promise(resolve =>
setTimeout(resolve, 5000)
);
return new Promise(resolve =>
setTimeout(() => resolve(value), 5000)
);
return promise.then(value =>
new Promise(resolve =>
setTimeout(() => resolve(value), 5000)
)
);
Bluebird promise library has better performance and convenient features that can be used out of the box to delay promise chains.
undefined
return Bluebird.delay(5000);
return Bluebird.resolve(value).delay(5000);
// or
return Bluebird.delay(5000).return(value);
return bluebirdPromise.delay(5000);
RxJS is already used in Angular 2/4 projects and can be used to create or transform promises with RxJS operators and small overhead.
undefined
return Observable.of().delay(5000).toPromise();
// or
return Observable.interval(5000).first().toPromise();
return Observable.of(value).delay(5000).toPromise();
return Observable.fromPromise(promise).delay(5000).toPromise();
You can use the following code to delay values in RxJs 6 by n
ms.
0
)return timer(n);
return of(value).pipe(delay(n));
or
return timer(n).pipe(mapTo(value));
return from(promise).pipe(delay(n));
put .toPromise()
on any of the previous examples after the pipe
.
return timer(n).toPromise();
return of(value).pipe(delay(n)).toPromise();
etc.