本文整理匯總了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);
}
}