All files / src/internal/shared utils.js

68.08% Statements 32/47
100% Branches 6/6
80% Functions 4/5
68.08% Lines 32/47

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 482x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 401x 401x 2x 2x 2x 321x 321x 2x 2x 2x 2334x 2424x 2424x 2334x 2x 2x 2x 2x 2x 2x 2x                                
export const noop = () => {};
 
// Adapted from https://github.com/then/is-promise/blob/master/index.js
// Distributed under MIT License https://github.com/then/is-promise/blob/master/LICENSE
 
/**
 * @template [T=any]
 * @param {any} value
 * @returns {value is PromiseLike<T>}
 */
export function is_promise(value) {
	return typeof value?.then === 'function';
}
 
/** @param {Function} fn */
export function run(fn) {
	return fn();
}
 
/** @param {Array<() => void>} arr */
export function run_all(arr) {
	for (var i = 0; i < arr.length; i++) {
		arr[i]();
	}
}
 
/**
 * TODO replace with Promise.withResolvers once supported widely enough
 * @template T
 * @returns {PromiseWithResolvers<T>}
 */
export function deferred() {
	/** @type {(value: T) => void} */
	var resolve;

	/** @type {(reason: any) => void} */
	var reject;

	/** @type {Promise<T>} */
	var promise = new Promise((res, rej) => {
		resolve = res;
		reject = rej;
	});

	// @ts-expect-error
	return { promise, resolve, reject };
}