Skip to content

Commit

Permalink
don't await non-objects returned from functions passed to `AsyncItera…
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Oct 20, 2022
1 parent 98edc05 commit 8e58a6d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `String.prototype.toWellFormed`
- Recent updates of the [iterator helpers proposal](https://github.com/tc39/proposal-iterator-helpers):
- Added a counter parameter to helpers, [proposal-iterator-helpers/211](https://github.com/tc39/proposal-iterator-helpers/pull/211)
- Don't await non-objects returned from functions passed to `AsyncIterator` helpers, [proposal-iterator-helpers/239](https://github.com/tc39/proposal-iterator-helpers/pull/239)
- Early exit on broken `.next` in missed cases of `{ Iterator, AsyncIterator }.from`, [proposal-iterator-helpers/232](https://github.com/tc39/proposal-iterator-helpers/pull/232)
- Added `inverse` option to `core-js-compat`, [#1119](https://github.com/zloirock/core-js/issues/1119)
- Added `format` option to `core-js-builder`, [#1120](https://github.com/zloirock/core-js/issues/1120)
Expand Down
16 changes: 11 additions & 5 deletions packages/core-js/internals/async-iterator-iteration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var doesNotExceedSafeInteger = require('../internals/does-not-exceed-safe-integer');
var getBuiltIn = require('../internals/get-built-in');
var getIteratorDirect = require('../internals/get-iterator-direct');
Expand Down Expand Up @@ -44,20 +45,25 @@ var createMethod = function (TYPE) {
var value = step.value;
try {
if (MAPPING) {
Promise.resolve(fn(value, counter)).then(function (result) {
var result = fn(value, counter);

var handler = function ($result) {
if (IS_FOR_EACH) {
loop();
} else if (IS_EVERY) {
result ? loop() : closeAsyncIteration(iterator, resolve, false, reject);
$result ? loop() : closeAsyncIteration(iterator, resolve, false, reject);
} else if (IS_TO_ARRAY) {
try {
target[counter++] = result;
target[counter++] = $result;
loop();
} catch (error4) { ifAbruptCloseAsyncIterator(error4); }
} else {
result ? closeAsyncIteration(iterator, resolve, IS_SOME || value, reject) : loop();
$result ? closeAsyncIteration(iterator, resolve, IS_SOME || value, reject) : loop();
}
}, ifAbruptCloseAsyncIterator);
};

if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
else handler(result);
} else {
target[counter++] = value;
loop();
Expand Down
10 changes: 8 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.filter.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
var createIterResultObject = require('../internals/create-iter-result-object');
Expand Down Expand Up @@ -34,9 +35,14 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(filterer(value, state.counter++)).then(function (selected) {
var result = filterer(value, state.counter++);

var handler = function (selected) {
selected ? resolve(createIterResultObject(value, false)) : loop();
}, ifAbruptCloseAsyncIterator);
};

if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
else handler(result);
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
}
} catch (error2) { doneAndReject(error2); }
Expand Down
10 changes: 8 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.flat-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
var createIterResultObject = require('../internals/create-iter-result-object');
Expand Down Expand Up @@ -36,13 +37,18 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(mapper(value, state.counter++)).then(function (mapped) {
var result = mapper(value, state.counter++);

var handler = function (mapped) {
try {
state.innerIterator = innerIterator = getAsyncIterator(mapped);
state.innerNext = aCallable(innerIterator.next);
innerLoop();
} catch (error4) { ifAbruptCloseAsyncIterator(error4); }
}, ifAbruptCloseAsyncIterator);
};

if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
else handler(result);
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
}
} catch (error2) { doneAndReject(error2); }
Expand Down
10 changes: 8 additions & 2 deletions packages/core-js/modules/esnext.async-iterator.map.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var getIteratorDirect = require('../internals/get-iterator-direct');
var createAsyncIteratorProxy = require('../internals/async-iterator-create-proxy');
var createIterResultObject = require('../internals/create-iter-result-object');
Expand Down Expand Up @@ -32,9 +33,14 @@ var AsyncIteratorProxy = createAsyncIteratorProxy(function (Promise) {
} else {
var value = step.value;
try {
Promise.resolve(mapper(value, state.counter++)).then(function (mapped) {
var result = mapper(value, state.counter++);

var handler = function (mapped) {
resolve(createIterResultObject(mapped, false));
}, ifAbruptCloseAsyncIterator);
};

if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
else handler(result);
} catch (error2) { ifAbruptCloseAsyncIterator(error2); }
}
} catch (error) { doneAndReject(error); }
Expand Down
12 changes: 9 additions & 3 deletions packages/core-js/modules/esnext.async-iterator.reduce.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var $ = require('../internals/export');
var call = require('../internals/function-call');
var aCallable = require('../internals/a-callable');
var anObject = require('../internals/an-object');
var isObject = require('../internals/is-object');
var getBuiltIn = require('../internals/get-built-in');
var getIteratorDirect = require('../internals/get-iterator-direct');
var closeAsyncIteration = require('../internals/async-iterator-close');
Expand Down Expand Up @@ -39,10 +40,15 @@ $({ target: 'AsyncIterator', proto: true, real: true, forced: true }, {
accumulator = value;
loop();
} else try {
Promise.resolve(reducer(accumulator, value, counter)).then(function (result) {
accumulator = result;
var result = reducer(accumulator, value, counter);

var handler = function ($result) {
accumulator = $result;
loop();
}, ifAbruptCloseAsyncIterator);
};

if (isObject(result)) Promise.resolve(result).then(handler, ifAbruptCloseAsyncIterator);
else handler(result);
} catch (error3) { ifAbruptCloseAsyncIterator(error3); }
}
counter++;
Expand Down

0 comments on commit 8e58a6d

Please sign in to comment.