UNPKG

2.13 kBJavaScriptView Raw
1Object.defineProperty(exports, "__esModule", { value: true });
2var iterall_1 = require("iterall");
3/**
4 * Given an AsyncIterable and a callback function, return an AsyncIterator
5 * which produces values mapped via calling the callback function.
6 */
7function mapAsyncIterator(iterator, callback, rejectCallback) {
8 var _a;
9 var $return;
10 var abruptClose;
11 if (typeof iterator.return === 'function') {
12 $return = iterator.return;
13 abruptClose = function (error) {
14 var rethrow = function () { return Promise.reject(error); };
15 return $return.call(iterator).then(rethrow, rethrow);
16 };
17 }
18 function mapResult(result) {
19 return result.done
20 ? result
21 : asyncMapValue(result.value, callback).then(iteratorResult, abruptClose);
22 }
23 var mapReject;
24 if (rejectCallback) {
25 // Capture rejectCallback to ensure it cannot be null.
26 var reject_1 = rejectCallback;
27 mapReject = function (error) {
28 return asyncMapValue(error, reject_1).then(iteratorResult, abruptClose);
29 };
30 }
31 return _a = {
32 next: function () {
33 return iterator.next().then(mapResult, mapReject);
34 },
35 return: function () {
36 return $return
37 ? $return.call(iterator).then(mapResult, mapReject)
38 : Promise.resolve({ value: undefined, done: true });
39 },
40 throw: function (error) {
41 if (typeof iterator.throw === 'function') {
42 return iterator.throw(error).then(mapResult, mapReject);
43 }
44 return Promise.reject(error).catch(abruptClose);
45 }
46 },
47 _a[iterall_1.$$asyncIterator] = function () {
48 return this;
49 },
50 _a;
51}
52exports.default = mapAsyncIterator;
53function asyncMapValue(value, callback) {
54 return new Promise(function (resolve) { return resolve(callback(value)); });
55}
56function iteratorResult(value) {
57 return { value: value, done: false };
58}
59//# sourceMappingURL=mapAsyncIterator.js.map
\No newline at end of file