Comparing version 0.0.3 to 0.0.4
101
index.js
@@ -1,3 +0,7 @@ | ||
function asyncy(functionOrObject,inlineCatch=false,target=functionOrObject) { | ||
const compile = (f) => { | ||
(function() { | ||
"use strict"; | ||
var undfnd; | ||
const UNDEFINED = undfnd; | ||
function asyncy(functionOrObject,inlineCatch=false,target=functionOrObject) { | ||
const compile = (f) => { | ||
return async function() { | ||
@@ -9,49 +13,58 @@ const me = this; | ||
type = typeof(functionOrObject); | ||
if(type==="function") return compile(functionOrObject); | ||
if(functionOrObject && type==="object") { | ||
for(let key in functionOrObject) { | ||
const token = key.substring(key.length-("Async").length), | ||
value = functionOrObject[key], | ||
type = typeof(value); | ||
if(token.indexOf("Sync")>0 || type!=="function") continue; | ||
target[key] = compile(functionOrObject[key]); | ||
if(type==="function") { return compile(functionOrObject); } | ||
if(functionOrObject && type==="object") { | ||
for(let key in functionOrObject) { | ||
const token = key.substring(key.length-("Async").length), | ||
value = functionOrObject[key], | ||
type = typeof(value); | ||
if(token.indexOf("Sync")>0 || type!=="function") { continue; } | ||
target[key] = compile(functionOrObject[key]); | ||
} | ||
return target; | ||
} | ||
return target; | ||
throw new TypeError("asyncy takes a function or a non-null object argument not a " + type); | ||
} | ||
throw new TypeError("asyncy takes a function or a non-null object argument not a " + type); | ||
} | ||
asyncy.inline = function (thisArg,f,...args) { | ||
const cb = (typeof(args[args.length-1])==="function" ? args.pop() : null); | ||
return new Promise((resolve,reject) => { | ||
try { | ||
let outerresult = f.call(thisArg,...args,(err,...result) => { | ||
outerresult = undefined; | ||
if(err) { | ||
resolve({err,args,result}); | ||
} else { | ||
if(cb) { | ||
try { | ||
cb(null,...result); | ||
} catch(err) { | ||
resolve({err,args:result}); | ||
asyncy.inline = function (thisArg,f,...args) { | ||
const cb = (typeof(args[args.length-1])==="function" ? args.pop() : null); | ||
return new Promise((resolve,reject) => { | ||
let outerresult | ||
try { | ||
outerresult = f.call(thisArg,...args,(err,...result) => { | ||
outerresult = UNDEFINED; | ||
if(err) { | ||
resolve({err,args,result}); | ||
} else { | ||
if(cb) { | ||
try { | ||
cb(null,...result); | ||
} catch(err) { | ||
resolve({err,args:result}); | ||
} | ||
} | ||
resolve({result}); | ||
} | ||
resolve({result}); | ||
}); | ||
if(!["nextTick","setTimeout","setInterval"].includes(f.name)) { | ||
setTimeout(() => { | ||
// assume a function that returns a value is not one that invokes a callback | ||
// there are exceptions, but the programmer will need to handle those | ||
if(outerresult!==UNDEFINED) { | ||
if(outerresult instanceof Promise) { outerresult.then(result => resolve({result})); } | ||
else if(outerresult instanceof Error) { resolve({err:outerresult}); } | ||
else { resolve({result:outerresult}); } | ||
} | ||
}); | ||
} | ||
}); | ||
if(!["nextTick","setTimeout","setInterval"].includes(f.name)) { | ||
setTimeout(() => { | ||
// assume a function that returns a value is not one that invokes a callback | ||
// there are exceptions, but the programmer will need to handle those | ||
if(outerresult!==undefined) { | ||
if(outerresult instanceof Promise) outerresult.then(result => resolve({result})); | ||
else if(outerresult instanceof Error) resolve({err:outerresult}); | ||
else resolve({result:outerresult}); | ||
} | ||
}); | ||
} catch(err) { | ||
resolve({err,args}); | ||
} | ||
} catch(err) { | ||
resolve({err,args}) | ||
} | ||
}); | ||
} | ||
}); | ||
} | ||
if(typeof(module)!=="undefined") { | ||
module.exports = asyncy; | ||
} | ||
if(typeof(window)!=="undefined") { | ||
window.asyncy = asyncy; | ||
} | ||
}).call(this); | ||
{ | ||
"name": "asyncy", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A utility for creating aysnc functions", | ||
@@ -8,3 +8,3 @@ "engines": {}, | ||
"scripts": { | ||
}, | ||
@@ -11,0 +11,0 @@ "repository": { |
@@ -90,2 +90,3 @@ # asyncy | ||
2017-07-07 v0.0.4 Codacy driven style, error and security improvements. | ||
@@ -92,0 +93,0 @@ 2017-06-03 v0.0.3 improved documentation |
// change to object order depended!!! | ||
var asyncy; | ||
if(typeof(module)!=="undefined") { | ||
asyncy = require("../index.js"); | ||
} | ||
const tests = { | ||
@@ -3,0 +7,0 @@ regularFunctionWithNoArgs: [function () { return true; },[]], |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
10395
103
97