UNPKG

ix

Version:

The Interactive Extensions for JavaScript

41 lines (39 loc) 1.48 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncify = void 0; const tslib_1 = require("tslib"); const asyncsink_js_1 = require("./asyncsink.js"); const memoize_js_1 = require("./operators/memoize.js"); /** * Converts the callback function into wrapped function which returns an async-iterable. * * @template TSource The type of the value returned from the callback. * @param {Function} func The callback function to wrap as an async-iterable. * @returns {(...args: any[]) => AsyncIterableX<TSource>} A function when invoked, returns an async-iterable from the callback. */ function asyncify(func) { return function (...args) { const sink = new asyncsink_js_1.AsyncSink(); const handler = function (...innerArgs) { sink.write(innerArgs.length === 1 ? innerArgs[0] : innerArgs); sink.end(); }; try { func(...args.concat(handler)); } catch (e) { sink.error(e); sink.end(); } const yielder = function () { return tslib_1.__asyncGenerator(this, arguments, function* () { for (let next; !(next = yield tslib_1.__await(sink.next())).done;) { yield yield tslib_1.__await(next.value); } }); }; return (0, memoize_js_1.memoize)()(yielder()); }; } exports.asyncify = asyncify; //# sourceMappingURL=asyncify.js.map