get-async-iterator-flattenable.js 1.0 KB

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