functional programming, 就是data transformation, functions are very easy to test. the idea is before you process the response received from the server side. do some pre-processing on them, like sanity check, even you can throw Error if the response is not what you expect. The idea is that you just pass a Promise around. A Promise has three state: unresolved, resolved, and rejected. There are many ways to create a Promise, using a constructor, new Promise(function(resolve, reject){ // do your processing here, once processing done // you can do resolve(value) // or reject(reason) }) You can directly do: Promise.resolve(value) or Promise.reject(reason) If something is a thenable, you can do: Promise.resolve(thenable) finally you can do promise.then(function(value){ }) .catch(function(err){ }) you can even catch specific errors with the help of bluebird. Because I know the response has been sanity checked, I can safely use it. Also Promise holds the exception too so I can do something else if something went wrong.
【在 f*****w 的大作中提到】 : 没很明白 能展开说说吗
p*2
27 楼
to Error req?.body?.body
【在 l**********n 的大作中提到】 : functional programming, 就是data transformation, functions are very easy to : test. : the idea is before you process the response received from the server side. : do some pre-processing on them, like sanity check, even you can throw Error : if the response is not what you expect. : The idea is that you just pass a Promise around. : A Promise has three state: unresolved, resolved, and rejected. : There are many ways to create a Promise, using a constructor, : new Promise(function(resolve, reject){ : // do your processing here, once processing done
l*n
28 楼
how you process exception in your way? Scala has Promise too.
【在 p*****2 的大作中提到】 : : to : Error : req?.body?.body