Add delay to javascript method that returns promise

后端 未结 2 574
醉酒成梦
醉酒成梦 2020-12-10 05:07

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

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 05:49

    Delayed result with RxJS 6

    You can use the following code to delay values in RxJs 6 by n ms.

    With no specific value (will emit 0)

    return timer(n);
    

    With a set value

    return of(value).pipe(delay(n));
    

    or

    return timer(n).pipe(mapTo(value));
    

    from a promise

    return from(promise).pipe(delay(n));
    

    to return a promise

    put .toPromise() on any of the previous examples after the pipe.

    return timer(n).toPromise();
    
    return of(value).pipe(delay(n)).toPromise();
    

    etc.

提交回复
热议问题