當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。