2017-10-15 64 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上發出錯誤。