当前位置: 首页>>代码示例>>TypeScript>>正文


TypeScript Observable.toPromise方法代码示例

本文整理汇总了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),
     );
 }
开发者ID:emjburns,项目名称:deck,代码行数:8,代码来源:AccountService.ts

示例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);
    }
}
开发者ID:reasonerjt,项目名称:harbor,代码行数:12,代码来源:utils.ts

示例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;
}
开发者ID:DevIntent,项目名称:angular-cli,代码行数:61,代码来源:parse_bep.ts


注:本文中的rxjs.Observable.toPromise方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。