Comparing version 2.3.0 to 2.4.0
49
index.js
@@ -21,14 +21,43 @@ 'use strict'; | ||
return function () { | ||
var async = false; | ||
var args = arguments; | ||
var promise = new Promise(function (resolve, reject) { | ||
var resolved = false; | ||
const wrappedResolve = function (value) { | ||
if (resolved) { | ||
console.warn('Run-async promise already resolved.') | ||
} | ||
resolved = true; | ||
resolve(value); | ||
} | ||
var rejected = false; | ||
const wrappedReject = function (value) { | ||
if (rejected) { | ||
console.warn('Run-async promise already rejected.') | ||
} | ||
rejected = true; | ||
reject(value); | ||
} | ||
var usingCallback = false; | ||
var callbackConflict = false; | ||
var contextEnded = false; | ||
var answer = func.apply({ | ||
async: function () { | ||
async = true; | ||
if (contextEnded) { | ||
console.warn('Run-async async() called outside a valid run-async context, callback will be ignored.'); | ||
return function() {}; | ||
} | ||
if (callbackConflict) { | ||
console.warn('Run-async wrapped function (async) returned a promise.\nCalls to async() callback can have unexpected results.'); | ||
} | ||
usingCallback = true; | ||
return function (err, value) { | ||
if (err) { | ||
reject(err); | ||
wrappedReject(err); | ||
} else { | ||
resolve(value); | ||
wrappedResolve(value); | ||
} | ||
@@ -39,9 +68,15 @@ }; | ||
if (!async) { | ||
if (usingCallback) { | ||
if (isPromise(answer)) { | ||
answer.then(resolve, reject); | ||
console.warn('Run-async wrapped function (sync) returned a promise but async() callback must be executed to resolve.'); | ||
} | ||
} else { | ||
if (isPromise(answer)) { | ||
callbackConflict = true; | ||
answer.then(wrappedResolve, wrappedReject); | ||
} else { | ||
resolve(answer); | ||
wrappedResolve(answer); | ||
} | ||
} | ||
contextEnded = true; | ||
}); | ||
@@ -48,0 +83,0 @@ |
{ | ||
"name": "run-async", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"description": "Utility method to run function either synchronously or asynchronously using the common `this.async()` style.", | ||
@@ -27,4 +27,4 @@ "main": "index.js", | ||
"devDependencies": { | ||
"mocha": "^3.1.2" | ||
"mocha": "^7.1.0" | ||
} | ||
} |
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
6537
83