-
-
Save jonathan-annett/1a8ae0cc504427317ba2912d2620aae5 to your computer and use it in GitHub Desktop.
promising code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isPromise(p) { | |
return !!p&&typeof p==='object'&&p.constructor===Promise; | |
} | |
function toPromise(p){ | |
return !!p&&typeof p==='object'&&p.constructor===Promise?p:typeof p==='function'&&p.length===2?new Promise(p):Promise.resolve(p); | |
} | |
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
write/convert functions that can be called expecting
a
cb(error,result)
ornew Promise (...)
formatpromiseToCB
converts and exports an existing function that's been previously coded to return a promisecbToPromise
converts and exports an existing function that's been previously coded to call the last argument with (error,result)cb(undefined,path,stat)
--->resolve([path,stat])
/cb(undefined,[path,stat])
asPromise
lets you code a new function to return a promise, but it can be called either wayasCallback
lets you code a new function to callcb(err,result)
, but it can be invoked either waysample functions
each sample takes 2 arguments, and resolves/rejects/errors based on random number.
arg2 can be also be used to force pass or fail. (looks for "-pass" or "-fail").
wrap existing functions
promiseToCB(function myFunc(){},newThis);
)or code new functions, which embed a wrapper.
test scipt for above functions