死灵一点点,这个链接可能有用....
TLDR;查看此答案末尾的代码段示例
编写/转换可以调用的函数
一个cb(error,result)
或new Promise (...)
格式
promiseToCB
转换和导出先前编码为返回承诺的现有函数
cbToPromise
转换和导出一个现有函数,该函数先前已编码为使用 (error,result) 调用最后一个参数
- 如果包装函数提供超过 1 个结果,则结果将是一个结果数组
- 例如
cb(undefined,path,stat)
---> resolve([path,stat])
/cb(undefined,[path,stat])
asPromise
允许您编写一个新函数来返回一个承诺,但它可以以任何一种方式调用
asCallback
允许您编写要调用的新函数cb(err,result)
,但可以通过任何一种方式调用它
示例函数
每个样本有 2 个参数,并根据随机数解决/拒绝/错误。
arg2 也可用于强制通过或失败。(寻找“-pass”或“-fail”)。
包装现有功能
- 将函数导出到当前的“this”(或使用
promiseToCB(function myFunc(){},newThis);
)
promiseToCB(function sampleFunc1(arg1,arg2) {
console.log("deciding:",arg1,arg2);
return new Promise(function(resolve,reject){
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});
});
cbToPromise('sampleFunc2',function someOtherName(arg1,arg2,cb) {
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
},local);
或编写嵌入包装器的新函数。
function sampleFunc3(arg1,arg2) {return asPromise(arguments,function(resolve,reject){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});}
function sampleFunc4(arg1,arg2) {return asCallback(arguments,function(cb){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
});}
测试上述功能的 scipt
const local = {};
promiseToCB(function sampleFunc1(arg1,arg2) {
console.log("deciding:",arg1,arg2);
return new Promise(function(resolve,reject){
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});
});
cbToPromise('sampleFunc2',function someOtherName(arg1,arg2,cb) {
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
},local);
function sampleFunc3(arg1,arg2) {return asPromise(arguments,function(resolve,reject){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});}
function sampleFunc4(arg1,arg2) {return asCallback(arguments,function(cb){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
});}
const log=console.log.bind(console),info=console.info.bind(console),error=console.error.bind(console);
sampleFunc1("sample1","promise").then (log).catch(error);
local.sampleFunc2("sample2","promise").then (log).catch(error);
sampleFunc3("sample3","promise").then (log).catch(error);
sampleFunc4("sample4","promise").then (log).catch(error);
sampleFunc1("sample1","callback",info);
local.sampleFunc2("sample2","callback",info);
sampleFunc3("sample3","callback",info);
sampleFunc4("sample4","callback",info);
sampleFunc1("sample1","promise-pass").then (log).catch(error);
local.sampleFunc2("sample2","promise-pass").then (log).catch(error);
sampleFunc3("sample3","promise-pass").then (log).catch(error);
sampleFunc4("sample4","promise-pass").then (log).catch(error);
sampleFunc1("sample1","callback-pass",info);
local.sampleFunc2("sample2","callback-pass",info);
sampleFunc3("sample3","callback-pass",info);
sampleFunc4("sample4","callback-pass",info);
sampleFunc1("sample1","promise-fail").then (log).catch(error);
local.sampleFunc2("sample2","promise-fail").then (log).catch(error);
sampleFunc3("sample3","promise-fail").then (log).catch(error);
sampleFunc4("sample4","promise-fail").then (log).catch(error);
sampleFunc1("sample1","callback-fail",info);
local.sampleFunc2("sample2","callback-fail",info);
sampleFunc3("sample3","callback-fail",info);
sampleFunc4("sample4","callback-fail",info);
var cpArgs = Array.prototype.slice.call.bind(Array.prototype.slice);
function promiseToCB (nm,fn,THIS) {
if (typeof nm==='function') {
THIS=fn;fn=nm;nm=fn.name;
}
THIS=THIS||this;
const func = function () {
let args = cpArgs(arguments);
if (typeof args[args.length-1]==='function') {
const cb = args.pop();
return fn.apply(THIS,args).then(function(r){
cb (undefined,r);
}).catch(cb);
} else {
return fn.apply(THIS,args);
}
};
Object.defineProperty(func,'name',{value:nm,enumerable:false,configurable: true});
if (THIS[nm]) delete THIS[nm];
Object.defineProperty(THIS,nm,{value:func,enumerable:false,configurable: true});
return func;
}
function cbToPromise (nm,fn,THIS) {
if (typeof nm==='function') {
THIS=fn;fn=nm;nm=fn.name;
}
THIS=THIS||this;
const func = function () {
let args = cpArgs(arguments);
if (typeof args[args.length-1]==='function') {
return fn.apply(THIS,args);
} else {
return new Promise(function(resolve,reject){
args.push(function(err,result){
if (err) return reject(err);
if (arguments.length==2) {
return resolve(result);
}
return resolve(cpArgs(arguments,1));
});
fn.apply(THIS,args);
});
}
};
Object.defineProperty(func,'name',{value:nm,enumerable:false,configurable: true});
if (THIS[nm]) delete THIS[nm];
Object.defineProperty(THIS,nm,{value:func,enumerable:false,configurable: true});
return func;
}
function asPromise (args,resolver,no_err) {
const cb = args[args.length-1],
promise = new Promise(resolver);
return (typeof cb==='function') ? promise.then(function(result){return cb(no_err,result)}).catch(cb) : promise;
}
function asCallback (args,wrap,no_err) {
const cb = args[args.length-1],
promise=new Promise(function resolver(resolve,reject) {
return wrap (function (err,result) {
if (err) return reject(err);
resolve(result);
});
});
return (typeof cb==='function') ? promise.then(function(result){return cb(no_err,result)}).catch(cb) : promise;
}
function cbPromiseTest(){
/*global sampleFunc1,sampleFunc2*/
const local = {};
promiseToCB(function sampleFunc1(arg1,arg2) {
console.log("deciding:",arg1,arg2);
return new Promise(function(resolve,reject){
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});
});
cbToPromise('sampleFunc2',function someOtherName(arg1,arg2,cb) {
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
},local);
function sampleFunc3(arg1,arg2) {return asPromise(arguments,function(resolve,reject){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){reject([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
resolve([arg1,arg2,"all good"].join("-"));
}
},2000);
});}
function sampleFunc4(arg1,arg2) {return asCallback(arguments,function(cb){
console.log("deciding:",arg1,arg2);
const timer = setTimeout(function(){cb([arg1,arg2,"ouch"].join("-"));},5000);
setTimeout(function(){
if (arg2.endsWith("-pass") || (!arg2.endsWith("-fail") && Math.random()<0.5)) {
console.log("complete:",arg1,arg2);
clearTimeout(timer);
cb(undefined,[arg1,arg2,"all good"].join("-"));
}
},2000);
});}
const log=console.log.bind(console),info=console.info.bind(console),error=console.error.bind(console);
sampleFunc1("sample1","promise").then (log).catch(error);
local.sampleFunc2("sample2","promise").then (log).catch(error);
sampleFunc3("sample3","promise").then (log).catch(error);
sampleFunc4("sample4","promise").then (log).catch(error);
sampleFunc1("sample1","callback",info);
local.sampleFunc2("sample2","callback",info);
sampleFunc3("sample3","callback",info);
sampleFunc4("sample4","callback",info);
sampleFunc1("sample1","promise-pass").then (log).catch(error);
local.sampleFunc2("sample2","promise-pass").then (log).catch(error);
sampleFunc3("sample3","promise-pass").then (log).catch(error);
sampleFunc4("sample4","promise-pass").then (log).catch(error);
sampleFunc1("sample1","callback-pass",info);
local.sampleFunc2("sample2","callback-pass",info);
sampleFunc3("sample3","callback-pass",info);
sampleFunc4("sample4","callback-pass",info);
sampleFunc1("sample1","promise-fail").then (log).catch(error);
local.sampleFunc2("sample2","promise-fail").then (log).catch(error);
sampleFunc3("sample3","promise-fail").then (log).catch(error);
sampleFunc4("sample4","promise-fail").then (log).catch(error);
sampleFunc1("sample1","callback-fail",info);
local.sampleFunc2("sample2","callback-fail",info);
sampleFunc3("sample3","callback-fail",info);
sampleFunc4("sample4","callback-fail",info);
}
cbPromiseTest();