get-async-iterator-flattenable.js 1.1 KB

123456789101112131415161718192021222324252627282930
  1. 'use strict';
  2. var call = require('../internals/function-call');
  3. var isCallable = require('../internals/is-callable');
  4. var anObject = require('../internals/an-object');
  5. var getIteratorDirect = require('../internals/get-iterator-direct');
  6. var getIteratorMethod = require('../internals/get-iterator-method');
  7. var getMethod = require('../internals/get-method');
  8. var wellKnownSymbol = require('../internals/well-known-symbol');
  9. var AsyncFromSyncIterator = require('../internals/async-from-sync-iterator');
  10. var ASYNC_ITERATOR = wellKnownSymbol('asyncIterator');
  11. module.exports = function (obj) {
  12. var object = anObject(obj);
  13. var alreadyAsync = true;
  14. var method = getMethod(object, ASYNC_ITERATOR);
  15. var iterator;
  16. if (!isCallable(method)) {
  17. method = getIteratorMethod(object);
  18. alreadyAsync = false;
  19. }
  20. if (method !== undefined) {
  21. iterator = call(method, object);
  22. } else {
  23. iterator = object;
  24. alreadyAsync = true;
  25. }
  26. anObject(iterator);
  27. return getIteratorDirect(alreadyAsync ? iterator : new AsyncFromSyncIterator(getIteratorDirect(iterator)));
  28. };