Giter Club home page Giter Club logo

Comments (3)

Jerry-Hong avatar Jerry-Hong commented on September 13, 2024

@joshua7v 哈囉,你需要先確定 Error 能拿到那個 request 的 url,之後程式碼大概會像下面這樣

const urls = [...]; // 你的 urls

function crawler(...urls) {
    return Observable.from(urls)
            .mergeMap(url => <promise of requests>, <concurrency>)
            .map(res => <data object needed>)
            .concatAll()
            .withLatestFrom(<db>, (x, y) => <query promise>)
            .concatAll()
}

crawler(...urls)
.catch(e => {
    // 先判斷是不是 request 發生錯誤
    if(...) {
        // 從 error 拿到發生錯誤的 request url
        return crawler(e.url).retry(3) // 可以設定 retry 幾次
    }
    // 如果不是要做什麼,寫在這
}) 
.subscribe(...)

from learn-rxjs-in-30-days.

joshua7v avatar joshua7v commented on September 13, 2024

@Jerry-Hong 謝謝

crawler(...urls)
.catch(e => {
    // 先判斷是不是 request 發生錯誤
    if(...) {
        // 從 error 拿到發生錯誤的 request url
        return crawler(e.url).retry(3) // 可以設定 retry 幾次
    }
    // 如果不是要做什麼,寫在這
}) 

catch 到錯誤后返回用發生錯誤的 url 創建的 observable 之後
執行完就 complete 了
原來的 observable 中的 requests 就不會執行了
是我哪裡寫的不對嗎

from learn-rxjs-in-30-days.

Jerry-Hong avatar Jerry-Hong commented on September 13, 2024

@joshua7v Sorry 我寫錯了,應該這麼做

const urls = [...]; // 你的 urls

function crawler(...urls) {
    return Observable.from(urls)
            .mergeMap(url => Observable.from(fetch(url))
                                .retry(3)
                                .catch(error => {
                                    // 這裡可以直接 log 或者傳到通訊頻道
                                    // 如果不想 一個一個發可以先暫存在一個 array 裡,等到全部 complete 再發送
                                    return Observable.empty();
                                }), <concurrency>)
            .map(res => <data object needed>)
            .concatAll()
            .withLatestFrom(<db>, (x, y) => <query promise>)
            .concatAll()
}

crawler(...urls)
.catch(e => {
    // 這裡處理整體錯誤
}) 
.subscribe(...)

catch return 的 observable 會取代掉原先的,所以後面的才會消失。

from learn-rxjs-in-30-days.

Related Issues (3)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.