<commit_before>"use strong";
"use strict";

module.exports = function maybePromiseFactory(promiseConstructor) {
	return function maybePromise(f, ...args) {
		let result;
		try {
			result = f.apply(null, args);
		} catch(e) {
			return promiseConstructor.reject(e);
		}

		if(result instanceof promiseConstructor) {
			return result;
		} else if(result instanceof Error) {
			return promiseConstructor.reject(result);
		} else {
			return promiseConstructor.resolve(result);
		}
	};
}<commit_msg>Add useAnyThenable option; don't reject if a synchronous function for returning an Error<commit_after>"use strong";
"use strict";

/**
 * Constructs a maybePromise function.
 * @param {class} promiseConstructor A Promise implementation (e.g. `Bluebird` or `Promise`)
 * @param {boolean} [useAnyThenable] If truthy, any Promise implementation is considered a promise; otherwise,
 * only instances of the `promiseConstructor` are considered promises.
 * @returns {Function} A function that takes a function and arguments. Invokes the function with arguments. If
 * the result of the function is a promise, it is returned. Otherwise, the result is converted to a promise.
 * If an error is thrown, a rejected promise is returned.
 */
module.exports = function maybePromiseFactory(promiseConstructor, useAnyThenable) {
	return function maybePromise(f, ...args) {
		let result;
		try {
			result = f.apply(null, args);
		} catch(e) {
			return promiseConstructor.reject(e);
		}

		if(result instanceof promiseConstructor || (useAnyThenable && result && typeof result.then === 'function')) {
			return result;
		} else {
			return promiseConstructor.resolve(result);
		}
	};
}
{'fraction_non_alphanumeric': 0.10504201680672269, 'fraction_numerical': 0.0, 'mean_word_length': 4.5198675496688745, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4449484', 'n_tokens_mistral': 490, 'n_tokens_neox': 452, 'n_words': 160}