Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v11 feedback: streams are not async iterable #1159

Closed
darksabrefr opened this issue Apr 14, 2020 · 1 comment
Closed

v11 feedback: streams are not async iterable #1159

darksabrefr opened this issue Apr 14, 2020 · 1 comment
Labels
bug Something does not work as it should ✭ help wanted ✭ regression Something does not work anymore

Comments

@darksabrefr
Copy link

darksabrefr commented Apr 14, 2020

It seems v11 streams are a little bit broken when used with async iteration:

'use strict';
const got = require('got');
(async () =>  {
	const timeout = setTimeout(() => {}, 10000);
	try {
		const streamResponse = got.stream('https://sindresorhus.com');

		for await (const chunk of streamResponse) {
			console.log('chunk from streamResponse');
		}
		console.log('end');
	} finally {
		clearInterval(timeout);
	}
})();

This doesn't output anything. Note there is no error, where in a situation of a non-implemented async iterator should throw a TypeError saying "streamResponse is not async iterable" (you can add streamResponse[Symbol.asyncIterator] = undefined; to show that).

Now, a functional version (I used node's streams Readable.wrap for simplicity but implementing the right Symbol.asyncIterator is surely better):

'use strict';
const got = require('got');
const {Readable} = require('stream');
(async () => {
	const timeout = setTimeout(() => {}, 10000);
	try {
		const gotStreamResponse = got.stream('https://sindresorhus.com');
		const streamResponse = new Readable().wrap(gotStreamResponse);

		for await (const chunk of streamResponse) {
			console.log('chunk from streamResponse');
		}
		console.log('end');
	} finally {
		clearInterval(timeout);
	}
})();

This one outputs results:

chunk from streamResponse
chunk from streamResponse
end

Link it to #1154

@szmarczak szmarczak added bug Something does not work as it should regression Something does not work anymore ✭ help wanted ✭ labels Apr 15, 2020
@szmarczak
Copy link
Collaborator

Fixed in e97cf7e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something does not work as it should ✭ help wanted ✭ regression Something does not work anymore
Projects
None yet
Development

No branches or pull requests

2 participants