当一个observable中出现错误时,ConcatMap会中断

时间:2017-06-06 06:30:55

标签: rxjs observable concatmap

我在下面添加了我的代码

function trimString($haystack, $length, $search, $insert = ''){
    $str_length = strlen($haystack);

    if ($str_length <= $length){
        return $haystack;
    }

    $from = 0;
    $string_value = '';
    $search_after = $length;

    // find the last position of $search in the string
    $pos_last_dot = strrpos($haystack, $search);

    while ($search_after <= $pos_last_dot) {
        // get the position of $search after the last position
        $pos = strpos($haystack , $search, $search_after);

        // append the $search and the $insert for a new line
        $string_value .= substr(substr($haystack, 0, $pos), $from) . $search . $insert;

        // set the cursor to the next position after the finding $search
        $from = $pos + 1;

        // add the last occurence of $search
        if (($from + $length) >= $pos_last_dot && $pos != $pos_last_dot) {
            $string_value .= substr(substr($haystack, 0, $pos_last_dot), $from) . $search . $insert;
            break;
        }

        $search_after = $from + $length;
    }

    return $string_value;
}

echo trimString("This is a test. This is not a test. This is a new test This is a new test. foo. bar.", 10, ".", "<br/>");

如果setdata为任何中间节点抛出错误,则下一个节点序列不会恢复。我们如何修改它,以便即使一个节点出现错误,也会执行下一个节点序列。我读过'onerrorResumeNext',但不知道如何在这里使用它? 任何帮助都会有用。

setdata()如下:

    `Observable.from(modifiedNodes)
                        .concatMap(node => {

                            return this.Model.setData(node);
                        })
                        .subscribe(
                        () => {
                            NodeSaved++;
                        }`

1 个答案:

答案 0 :(得分:3)

如果setData返回可观察对象,则可以使用catch运算符。它将收到setData内引发的任何错误,如果您返回一个空的observable,concatMap将移动到序列中的下一个节点:

import 'rxjs/add/observable/empty';
import 'rxjs/add/operator/catch';

Observable
  .from(modifiedNodes)
  .concatMap(node => this.Model
    .setData(node)
    .catch(error => Observable.empty())
  )
  .subscribe(() => { NodeSaved++; });