nodejs when 核心部分使用

来源:互联网 发布:plc与伺服电机编程 编辑:程序博客网 时间:2024/05/22 08:07
// 核心部分CORE


const when = require('when');


// 1.使用when直接构造一个promise
const test1 = when(1);
test1.then(d => {
console.log('1 == ', d);
});




// 2.when构造的promise成功时自动调用函数
const test2 = when(2, console.info);
test2.then(d => {
console.log('2 == ', d);
})


// 3.when是以es的promise包裹的, 你可以通过try在非es5环境下使用
const test3 = when.try(JSON.parse,
new Promise((res, rej) => {
setTimeout(() => res('{ "a": 3 }'), 3000);
})
);


test3.then(d => {
console.log('3 == ', d.a);
})


// 4.lift 其实lift在函数编程中出现得比较频繁, A => B  <=> A => f(B) [不知道有没有记错,哈哈]
const fn = nu => nu * nu;


const liftFN = when.lift(fn);
liftFN(4).then(d => {
console.log('4 == ', d);
})


// 5.join 类似Promise.all, 不过多解析


// 6.when.promise(function(resolve, reject, notify) {}) //这里也不过多解析 notify是不赞成使用的


// 7.isPromiseLike 判断是否支持then









0 0
原创粉丝点击