本文整理匯總了TypeScript中rxjs.Observable.toPromise方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript Observable.toPromise方法的具體用法?TypeScript Observable.toPromise怎麽用?TypeScript Observable.toPromise使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類rxjs.Observable
的用法示例。
在下文中一共展示了Observable.toPromise方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: listAllAccounts
public static listAllAccounts(provider: string = null, providerVersion: string = null): IPromise<IAccountDetails[]> {
return $q
.when(this.accounts$.toPromise())
.then((accounts: IAccountDetails[]) => accounts.filter(account => !provider || account.type === provider))
.then((accounts: IAccountDetails[]) =>
accounts.filter(account => !providerVersion || account.providerVersion === providerVersion),
);
}
示例2:
export function toPromise<T>(async: Observable<T> | Promise<T> | T): Promise<T> {
if (!async) {
return Promise.reject("Bad argument");
}
if (async instanceof Observable) {
let obs: Observable<T> = async;
return obs.toPromise();
} else {
return Promise.resolve(async);
}
}
示例3: main
async function main(argv: string[]): Promise<0|1> {
const s = new Subject<bes.BuildEvent>();
const o: Observable<bes.BuildEvent> = s.asObservable();
o.pipe(filterType('testSummary')).subscribe({
next: (evt: bes.BuildEvent) => {
if (!evt.id || !evt.id.testSummary || !evt.testSummary) {
throw new Error(
`expected BuildEvent to contain id, testSummary, id.testSummary
${JSON.stringify(evt)}`);
}
updateUi('Test result', {
label: evt.id.testSummary.label,
overallStatus: evt.testSummary.overallStatus,
});
}
});
o.pipe(filterType('buildFinished')).subscribe({
next: (evt: bes.BuildEvent) => {
if (!evt.finished || !evt.finished.exitCode) {
throw new Error(
`expected BuildEvent to contain finished, finished.exitCode
${JSON.stringify(evt)}`);
}
const exitName = evt.finished.exitCode.name;
switch (exitName) {
case 'SUCCESS':
updateUi('Thumbs up');
break;
case 'TESTS_FAILED':
updateUi('Some tests failed');
break;
case 'INTERRUPTED':
updateUi('Cancelled');
break;
case 'PARSING_FAILURE':
updateUi('Error in build configuration file, report to expert', {
someDebugThatsUsefulToTheExpert: 'a-file-path',
});
break;
default:
throw new Error(`Unhandled exitName ${exitName}`);
}
},
error: (e) => console.error('Unrecoverable error ', e)
});
// Push values into the subject
// TODO: should be streaming from file
// const stream = fs.createReadStream(argv[0]);
// stream.on('data')
// stream.on('end)
// etc.
const content = fs.readFileSync(argv[0], {'encoding': 'utf-8'});
content.split(/[\r\n]+/).filter(l => !!l).forEach(
evt => s.next(JSON.parse(evt)));
s.complete();
await o.toPromise();
return 0;
}