2017-10-15 38 views
0

比方说,我想流发出在某些情况下的错误:如何从xstream Stream上的.map()发送错误?

import xs from 'xstream' 
//... 
function transformBlah(blah) { 
    if (blah.status >= 200 && blah.status < 300) { 
     return blah.body 
    } else { 
     return new Error('I would like to send an error onto the Stream here') 
    } 
} 
const blahTransformed$ = xs.create(new BlahProducer()) 
          .map(transformBlah) 
+0

https://github.com/staltz/xstream#throw – bloodyKnuckles

+0

@bloodyKnuckles谢谢你,但是我怎么在当前Stream上做'.map'ped呢?还是从'transformBlah'返回一个新的流只是工作? –

回答

0

我发现,引发异常两个rxjsxstream实现这一点:

import xs from 'xstream' 
//... 
const blahTransformed$ = xs.create(new BlahProducer()) 
          .map(blah => { 
           if (blah.status >= 200 && blah.status < 300) { 
            return blah.body 
           } else { 
            throw blah.status 
           } 
          }) 

该例外被捕获通过Observable库,并在Stream上发出错误。