2023-12-18 13:12:25 +08:00
|
|
|
'use strict';
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
function isPromise(obj) {
|
|
|
|
return !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
|
|
|
|
}
|
2023-12-18 13:12:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a function that will run a function asynchronously or synchronously
|
|
|
|
*
|
|
|
|
* example:
|
|
|
|
* runAsync(wrappedFunction, callback)(...args);
|
|
|
|
*
|
|
|
|
* @param {Function} func Function to run
|
|
|
|
* @param {Function} cb Callback function passed the `func` returned value
|
|
|
|
* @return {Function(arguments)} Arguments to pass to `func`. This function will in turn
|
|
|
|
* return a Promise (Node >= 0.12) or call the callbacks.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var runAsync = module.exports = function (func, cb) {
|
|
|
|
cb = cb || function () {};
|
|
|
|
|
|
|
|
return function () {
|
2023-12-28 23:41:32 +08:00
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
var args = arguments;
|
|
|
|
|
|
|
|
var promise = new Promise(function (resolve, reject) {
|
2023-12-28 23:41:32 +08:00
|
|
|
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;
|
|
|
|
|
2023-12-18 13:12:25 +08:00
|
|
|
var answer = func.apply({
|
|
|
|
async: function () {
|
2023-12-28 23:41:32 +08:00
|
|
|
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;
|
2023-12-18 13:12:25 +08:00
|
|
|
return function (err, value) {
|
|
|
|
if (err) {
|
2023-12-28 23:41:32 +08:00
|
|
|
wrappedReject(err);
|
2023-12-18 13:12:25 +08:00
|
|
|
} else {
|
2023-12-28 23:41:32 +08:00
|
|
|
wrappedResolve(value);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}, Array.prototype.slice.call(args));
|
|
|
|
|
2023-12-28 23:41:32 +08:00
|
|
|
if (usingCallback) {
|
|
|
|
if (isPromise(answer)) {
|
|
|
|
console.warn('Run-async wrapped function (sync) returned a promise but async() callback must be executed to resolve.');
|
|
|
|
}
|
|
|
|
} else {
|
2023-12-18 13:12:25 +08:00
|
|
|
if (isPromise(answer)) {
|
2023-12-28 23:41:32 +08:00
|
|
|
callbackConflict = true;
|
|
|
|
answer.then(wrappedResolve, wrappedReject);
|
2023-12-18 13:12:25 +08:00
|
|
|
} else {
|
2023-12-28 23:41:32 +08:00
|
|
|
wrappedResolve(answer);
|
2023-12-18 13:12:25 +08:00
|
|
|
}
|
|
|
|
}
|
2023-12-28 23:41:32 +08:00
|
|
|
contextEnded = true;
|
2023-12-18 13:12:25 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
promise.then(cb.bind(null, null), cb);
|
|
|
|
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
runAsync.cb = function (func, cb) {
|
|
|
|
return runAsync(function () {
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
|
|
|
if (args.length === func.length - 1) {
|
|
|
|
args.push(this.async());
|
|
|
|
}
|
|
|
|
return func.apply(this, args);
|
|
|
|
}, cb);
|
|
|
|
};
|