1 Star 0 Fork 0

xiong/node-zookeeper-dubbo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
utils.js 1.21 KB
一键复制 编辑 原始数据 按行查看 历史
omnip 提交于 2018-10-30 17:46 . feat: return err if connection end by peer
"use strict";
const chalk = require("chalk");
let nextTick;
if (typeof setImmediate === "function") {
nextTick = setImmediate;
} else if (typeof process === "object" && process && process.nextTick) {
nextTick = process.nextTick;
} else {
nextTick = cb => {
setTimeout(cb, 0);
};
}
function nodeify(promise, cb) {
if (typeof cb !== "function") {
return promise;
}
return promise
.then(res => {
nextTick(() => {
cb(null, res);
});
})
.catch(err => {
cb(err);
});
}
function nodeifySelf(cb) {
return nodeify(this, cb);
}
Object.assign(Promise.prototype, {
nodeify: nodeifySelf
});
const co = genfun => {
const gen = genfun();
const next = value => {
const ret = gen.next(value);
if (ret.done) return;
ret.value((err, val) => {
if (err) {
return console.error(err);
}
return next(val);
});
};
next();
};
const log = (color, args) => console.log(chalk[color](args));
const print = Object.create(null);
print.warn = (...args) => log("yellow", args);
print.err = (...args) => log("red", args);
print.info = (...args) => log("green", args);
const noop = () => {};
module.exports = {
nodeify,
co,
print,
noop
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hacker_xwj/node-zookeeper-dubbo.git
git@gitee.com:hacker_xwj/node-zookeeper-dubbo.git
hacker_xwj
node-zookeeper-dubbo
node-zookeeper-dubbo
master

搜索帮助