Promise 反模式

783 查看

Promise 反模式

Promises are about making asynchronous code retain most of the lost properties of synchronous code such as flat indentation and one exception channel. – Bluebird Wiki: Promise Anti Patterns

Promises 是为了让异步代码也能保持这些同步代码的属性:扁平缩进和单异常管道。

Deferred 反模式

这种反模式中,deferred 对象的创建是没有意义的,反而会增加代码的复杂度。

例如:

这里的 deferred 对象并没有什么意义,而且可能在出错的情况下无法捕获。

正确的写法应该为:

再举一个例子: