Skip to content
Discussion options

You must be logged in to vote

theres no single event for "final completion", but you can handle it by recursivly listening to the retry event. when a stream completes without emitting retry, thats your final completion.

heres the pattern:

import got from 'got';

function attachListeners(stream) {
  stream.on('end', () => {
    // This fires for each attempt, including retries
    console.log('Stream ended (might retry)');
  });
  
  stream.on('retry', (retryCount, error, createRetryStream) => {
    console.log(`Retrying (attempt ${retryCount})`);
    const newStream = createRetryStream();
    
    // Recursivley attach listeners to the new stream
    attachListeners(newStream);
  });
  
  stream.on('close', () => {
    

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by sindresorhus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants