Skip to content

Conversation

szmarczak
Copy link
Collaborator

@szmarczak szmarczak commented Aug 1, 2020

Fixes #1381

const got = require('got');
const fs = require('fs');

let writeStream;

const fn = (retryCount = 0) => {
	const stream = got.stream('https://example.com');
	stream.retryCount = retryCount;

	if (writeStream) {
		writeStream.destroy();
	}

	writeStream = fs.createWriteStream('example.com');

	stream.pipe(writeStream);

	// If you don't attach the listener, it will NOT make a retry.
	// It automatically checks the listener count so it knows whether to retry or not :)
	stream.once('retry', fn);
};

fn();

Checklist

  • I have read the documentation.
  • I have included a pull request description of my changes.
  • I have included some tests.
  • If it's a new feature, I have included documentation updates.

@szmarczak
Copy link
Collaborator Author

@epicfaace Will you be happy with the API above?

@epicfaace
Copy link

Yes, that looks good! Perhaps retryCount could be specified in the got.stream method options, but otherwise it's great.

@szmarczak
Copy link
Collaborator Author

Perhaps retryCount could be specified in the got.stream method options

No, because when you have defined the options as a constant, then you would need to do {...options, retryCount} which would lead to a significant performance drop.

@szmarczak szmarczak marked this pull request as ready for review August 6, 2020 19:30
@szmarczak szmarczak merged commit 7072198 into sindresorhus:master Aug 6, 2020
@szmarczak szmarczak deleted the retry-stream branch August 6, 2020 19:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add retrying for the stream API
2 participants