第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

繼續(xù)在 RxJs 中使用 mergeMap 可管道化的錯誤

繼續(xù)在 RxJs 中使用 mergeMap 可管道化的錯誤

慕村225694 2021-11-12 15:27:04
我正在用 RxJs 管道和 mergeMap 操作符做一些并行的 HTTP get。在第一個請求失敗時(讓我們想象 /urlnotexists 拋出 404 錯誤)它會停止所有其他請求。我希望它繼續(xù)查詢所有剩余的 url,而不為這個失敗的請求調(diào)用所有剩余的 mergeMap。我嘗試使用來自 RxJs 的 throwError 和 catchError 但沒有成功。索引.jsconst { from } = require('rxjs');const { mergeMap, scan } = require('rxjs/operators');const request = {  get: url => {    return new Promise((resolve, reject) => {      setTimeout(() => {        if (url === '/urlnotexists') { return reject(new Error(url)); }        return resolve(url);      }, 1000);    });  }};(async function() {  await from([    '/urlexists',    '/urlnotexists',    '/urlexists2',    '/urlexists3',  ])    .pipe(      mergeMap(async url => {        try {          console.log('mergeMap 1:', url);          const val = await request.get(url);          return val;        } catch(err) {          console.log('err:', err.message);          // a throw here prevent all remaining request.get() to be tried        }      }),      mergeMap(async val => {        // should not pass here if previous request.get() failed         console.log('mergeMap 2:', val);        return val;      }),      scan((acc, val) => {        // should not pass here if previous request.get() failed         acc.push(val);        return acc;      }, []),    )    .toPromise()    .then(merged => {      // should have merged /urlexists, /urlexists2 and /urlexists3      // even if /urlnotexists failed      console.log('merged:', merged);    })    .catch(err => {      console.log('catched err:', err);    });})();$ node index.jsmergeMap 1: /urlexistsmergeMap 1: /urlnotexistsmergeMap 1: /urlexists2mergeMap 1: /urlexists3err: /urlnotexistsmergeMap 2: /urlexistsmergeMap 2: undefined <- I didn't wanted this mergeMap to have been calledmergeMap 2: /urlexists2mergeMap 2: /urlexists3merged: [ '/urlexists', undefined, '/urlexists2', '/urlexists3' ]我希望在最后發(fā)出并發(fā) GET 請求并減少它們各自在一個對象中的值。但是如果發(fā)生一些錯誤,我希望他們不要中斷我的管道,而是記錄它們。有什么建議嗎?
查看完整描述

2 回答

?
慕碼人2483693

TA貢獻(xiàn)1860條經(jīng)驗 獲得超9個贊

如果你想使用 RxJS,你應(yīng)該在catchError與forkJoin.


const { of, from, forkJoin } = rxjs;

const { catchError, tap } = rxjs.operators;


// your promise factory, unchanged (just shorter)

const request = {

  get: url => {

    return new Promise((resolve, reject) => setTimeout(

      () => url === '/urlnotexists' ? reject(new Error(url)) : resolve(url), 1000

    ));

  }

};


// a single rxjs request with error handling

const fetch$ = url => {

  console.log('before:', url);

  return from(request.get(url)).pipe(

    // add any additional operator that should be executed for each request here

    tap(val => console.log('after:', val)),

    catchError(error => {

      console.log('err:', error.message);

      return of(undefined);

    })

  );

};


// concurrently executed rxjs requests

forkJoin(["/urlexists", "/urlnotexists", "/urlexists2", "/urlexists3"].map(fetch$))

  .subscribe(merged => console.log("merged:", merged));

<script src="https://unpkg.com/@reactivex/rxjs@6.5.3/dist/global/rxjs.umd.js"></script>


查看完整回答
反對 回復(fù) 2021-11-12
?
至尊寶的傳說

TA貢獻(xiàn)1789條經(jīng)驗 獲得超10個贊

如果你愿意放棄 RXJS 而只是用 async/await 解決它是非常簡單的:


const urls = ['/urlexists', '/urlnotexists', '/urlexists2', '/urlexists3']

const promises = urls.map(url => request(url)

const resolved = await Promise.allSettled(promises)


// print out errors

resolved.forEach((r, i) => {

  if (r.status === "rejected') {

    console.log(`${urls[i]} failed: ${r.reason})

  }

})


// get the success results

const merged = resolved.filter(r => r.status === "resolved").map(r => r.value)

console.log('merged', merged)

這利用了Promise.allSettled提議的輔助方法。如果您的環(huán)境沒有此方法,則可以按照此答案中所示實現(xiàn)它。


查看完整回答
反對 回復(fù) 2021-11-12
  • 2 回答
  • 0 關(guān)注
  • 144 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號