mirror of
				https://github.com/therootcompany/request.js
				synced 2025-11-04 06:02:45 +00:00 
			
		
		
		
	docs: clarify stream usage
This commit is contained in:
		
							parent
							
								
									95a12a8285
								
							
						
					
					
						commit
						5149bc9dcb
					
				
							
								
								
									
										21
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										21
									
								
								README.md
									
									
									
									
									
								
							@ -48,14 +48,29 @@ In order to keep this library lightweight, performant, and keep the code easy to
 | 
				
			|||||||
read, the streaming behavior is **_slightly different_** from that of
 | 
					read, the streaming behavior is **_slightly different_** from that of
 | 
				
			||||||
`request.js`.
 | 
					`request.js`.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```diff
 | 
				
			||||||
 | 
					-var request = require('request');
 | 
				
			||||||
 | 
					+var request = require('@root/request');
 | 
				
			||||||
 | 
					 
 | 
				
			||||||
 | 
					-var stream = request({ url, headers });
 | 
				
			||||||
 | 
					+var stream = await request({ url, headers });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					 let attachment = await new MailgunAPI.Attachment({
 | 
				
			||||||
 | 
					   data: stream
 | 
				
			||||||
 | 
					 })
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Example:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```js
 | 
					```js
 | 
				
			||||||
var request = require('@root/request');
 | 
					var request = require('@root/request');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var resp = await request({
 | 
					var resp = await request({
 | 
				
			||||||
    url: 'http://www.google.com',
 | 
					    url: 'http://www.google.com',
 | 
				
			||||||
    stream: true
 | 
					    stream: true // true | 'filename.ext' | stream.Writable
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 'resp' itself is a ReadableStream
 | 
				
			||||||
resp.on('data', function () {
 | 
					resp.on('data', function () {
 | 
				
			||||||
    // got some data
 | 
					    // got some data
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
@ -64,8 +79,8 @@ resp.on('end', function () {
 | 
				
			|||||||
    // the data has ended
 | 
					    // the data has ended
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// resp.stream is a Promise that is resolved when the read stream is destroyed
 | 
					// 'resp.stream' is a Promise that is resolved when the read stream is destroyed
 | 
				
			||||||
await resp.stream;
 | 
					await resp.stream; // returns `undefined`
 | 
				
			||||||
console.log('Done');
 | 
					console.log('Done');
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user