@augment-vir/common
Version:
A collection of augments, helpers types, functions, and classes for any JavaScript environment.
32 lines (31 loc) • 854 B
JavaScript
import { DeferredPromise } from '@augment-vir/core';
/**
* Call a function asynchronously without interrupting current synchronous execution, even if the
* function was originally synchronous.
*
* @category Function
* @category Package : @augment-vir/common
* @example
*
* ```ts
* import {callAsynchronously} from '@augment-vir/common';
*
* console.info('1');
* const later = callAsynchronously(() => {
* console.info('3');
* });
* console.info('2');
* await later;
*
* // logs 1,2,3 in numeric order
* ```
*
* @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
*/
export async function callAsynchronously(callback) {
const deferredPromise = new DeferredPromise();
setTimeout(async () => {
deferredPromise.resolve(await callback());
});
return deferredPromise.promise;
}