@tmorin/ceb-messaging-simple
Version:
The package is part of the `<ceb/>` library. It provides an implementation of the messaging model leveraging on a vanilla TypeScript/JavaScript environment.
31 lines • 1.48 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
/**
* The method wraps a promise fulfillment in order to _cancel_ its resolution when a timeout is reached.
* @param fn the provide of the promise
* @param timeout the maximum time to wait for in millisecond
*/
export function waitForReturn(fn, timeout) {
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
reject(new Error(`unable to get the result on time`));
}, timeout);
Promise.resolve((() => __awaiter(this, void 0, void 0, function* () { return fn(); }))())
.then((output) => {
clearTimeout(timeoutId);
resolve(output);
})
.catch((error) => {
clearTimeout(timeoutId);
reject(error);
});
});
}
//# sourceMappingURL=common.js.map