本文整理汇总了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;
}