UNPKG

@reactivex/ix-es2015-cjs

Version:

The Interactive Extensions for JavaScript

47 lines (45 loc) 1.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.asyncifyErrback = void 0; const tslib_1 = require("tslib"); const asyncsink_js_1 = require("./asyncsink.js"); const memoize_js_1 = require("./operators/memoize.js"); /** * Converts a Node.js style error first callback to an async-iterable sequence. * * @template TSource The type of the returned value from the callback. * @param {Function} func The Node.js error first callback to convert to an async-iterable. * @returns {(...args: any[]) => AsyncIterableX<TSource>} A function, when invoked, contains the result of the callback as an async-iterable. */ function asyncifyErrback(func) { return function (...args) { const sink = new asyncsink_js_1.AsyncSink(); const handler = function (err, ...innerArgs) { if (err) { sink.error(err); sink.end(); } else { 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.asyncifyErrback = asyncifyErrback; //# sourceMappingURL=asyncifyerrback.js.map