一个比较简洁的Promise包装
一直觉得Promise的语法有些烦琐,且与NodeJS的原生API风格不兼容。发现ES6支持了Proxy,于是尝试写了一个模块promise-from来简化。这个模块有以下特性:
- 简洁的语法
- 链式访问
- 兼容Promise本身语法
- 支持传递
使用
安装
npm install -S promise-from
使用
const fs = require('fs');
const promiseFrom = require('promise-from');
promiseFrom(fs.createWriteStream('hello'))
.expect('open')
.write('hello')
.write('promise')
.then(function() {
console.log('This is good promise');
})
.catch(function(err) {
console.error(err);
});
接口
.expect(good, bad='error')
等待事件,发生`good`时,Promise解决,`bad`时拒绝