本文整理汇总了TypeScript中rxjs.Observable.defer方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Observable.defer方法的具体用法?TypeScript Observable.defer怎么用?TypeScript Observable.defer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类rxjs.Observable
的用法示例。
在下文中一共展示了Observable.defer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: loadWithFetch
export function loadWithFetch(url) {
return Observable.defer(() => {
return Observable.fromPromise(fetch(url).then(r => {
if (r.status === 200) {
return r.json();
} else {
return Promise.reject(r);
}
}))
}).retryWhen(retryStrategy());
}
示例2: loadWithFetch
export function loadWithFetch(url: string) {
return Observable.defer(() =>
{
return Observable
.fromPromise(fetch(url).then(r => {
if(r.status === 200) {
return r.json();
} else {
return Promise.reject(r);
}
}));
}).retryWhen(retryStrategy({attempts: 4, delay:1000}));
}
示例3: initialize
public static initialize(): void {
this.accounts$ = Observable.defer(() => {
const promise = API.one('credentials')
.useCache()
.withParams({ expand: true })
.get();
return Observable.fromPromise<IAccountDetails[]>(promise);
})
.publishReplay(1)
.refCount();
this.providers$ = AccountService.accounts$.map((accounts: IAccountDetails[]) => {
const providersFromAccounts: string[] = uniq(accounts.map(account => account.type));
return intersection(providersFromAccounts, CloudProviderRegistry.listRegisteredProviders());
});
}
示例4: waterfall
/**
* Make Async to Sync (like a waterfall)
*
* !!! NOT RECOMMENDED !!!. Do if necessary.
*
* @author mattpodwysocki
* @see https://github.com/Reactive-Extensions/RxJS/issues/73#issuecomment-30093431
*/
public static waterfall(series: ((fn?: any) => Observable<any>)[]): Observable<any> {
// tslint:disable-next-line:only-arrow-functions
return Observable.defer(function () {
let acc = series[0]();
for (let i = 1, len = series.length; i < len; i++) {
// tslint:disable-next-line:only-arrow-functions
(function (func) {
// tslint:disable-next-line:only-arrow-functions
acc = acc.flatMap(function (x) {
return func(x);
});
}(series[i]));
}
return acc;
});
}
示例5: execAndWaitForOutputToMatch
export function execAndWaitForOutputToMatch(cmd: string, args: string[], match: RegExp) {
if (cmd === 'ng' && args[0] === 'serve') {
// Accept matches up to 20 times after the initial match.
// Useful because the Webpack watcher can rebuild a few times due to files changes that
// happened just before the build (e.g. `git clean`).
// This seems to be due to host file system differences, see
// https://nodejs.org/docs/latest/api/fs.html#fs_caveats
return Observable.fromPromise(_exec({ waitForMatch: match }, cmd, args))
.concat(
Observable.defer(() =>
Observable.fromPromise(waitForAnyProcessOutputToMatch(match, 2500))
.repeat(20)
.catch(_x => Observable.empty())
)
)
.takeLast(1)
.toPromise();
} else {
return _exec({ waitForMatch: match }, cmd, args);
}
}