Repository: sindresorhus/got (ts)

Issue:
Request continues after cancellation
```javascript
const got = require('got');

(async function() {
	var promise = got('https://goo.gl/iiJ61G').on('redirect', resp => {
		console.log('Cancelling: ', resp.url);
		promise.cancel();
	}).on('downloadProgress', progress => {
		console.log(progress);
	});

	try {
		var request = await promise;
	} catch (e) {
		console.log(e);
	}
	
	console.log('Da end.');
})();
```

The following gives:
```
Cancelling:  https://goo.gl/iiJ61G
{ CancelError: Promise was canceled
    at PCancelable.cancel (C:\Users\Szymon Marczak\Desktop\ak server\node_modules\p-cancelable\index.js:66:16)
    at EventEmitter.got.on.resp (C:\Users\Szymon Marczak\Desktop\ak server\test.js:6:11)
    at EventEmitter.emit (events.js:159:13)
    at EventEmitter.emit (events.js:159:13)
    at res (C:\Users\Szymon Marczak\Desktop\ak server\node_modules\got\index.js:138:8)
    at ClientRequest.handler (C:\Users\Szymon Marczak\Desktop\ak server\node_modules\cacheable-request\src\index.js:88:7)
    at Object.onceWrapper (events.js:254:19)
    at ClientRequest.emit (events.js:159:13)
    at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:550:21)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:117:23) name: 'CancelError' }
Da end.
Cancelling:  http://google.pl/
{ percent: 0, transferred: 0, total: 5001 }
{ percent: 0.14917016596680663, transferred: 746, total: 5001 }
{ percent: 0.7210557888422315, transferred: 3606, total: 5001 }
{ percent: 0.9630073985202959, transferred: 4816, total: 5001 }
{ percent: 1, transferred: 6211, total: 5001 }
{ percent: 1, transferred: 16726, total: 5001 }
```

I think it should *really* abort the request.

Calling `promise.cancel()` -> `req.abort()` makes an error:
```javascript
{ Error: socket hang up
    at createHangUpError (_http_client.js:330:15)
    at Socket.socketCloseListener (_http_client.js:362:23)
    at Socket.emit (events.js:164:20)
    at TCP._handle.close [as _onclose] (net.js:558:12) code: 'ECONNRESET' }
```
which is catched [here](https://github.com/sindresorhus/got/blob/master/index.js#L210). Unfortunately, the request carries on because of [here](https://github.com/floatdrop/is-retry-allowed/blob/master/index.js#L5). It doesn't know who has hanged up, we or the server...

Pull request:
Don't continue the request after cancellation
Fixes #462

Patch:
diff --git a/index.js b/index.js
index 8bf252f..2ca8862 100644
--- a/index.js
+++ b/index.js
@@ -160,9 +160,18 @@ function requestAsEventEmitter(opts) {
 		});
 
 		cacheReq.once('request', req => {
+			let aborted = false;
+			req.once('abort', _ => {
+				aborted = true;
+			});
+
 			req.once('error', err => {
 				clearInterval(progressInterval);
 
+				if (aborted) {
+					return;
+				}
+
 				const backoff = opts.retries(++retryCount, err);
 
 				if (backoff) {


Test patch:
diff --git a/test/cancel.js b/test/cancel.js
index 016a7b7..4ab01b3 100644
--- a/test/cancel.js
+++ b/test/cancel.js
@@ -21,14 +21,43 @@ async function createAbortServer() {
 				res.end();
 			});
 		});
+
+		s.on('/redirect', (req, res) => {
+			res.writeHead(302, {
+				location: `${s.url}/abort`
+			});
+			res.end();
+
+			ee.emit('sentRedirect');
+
+			setTimeout(resolve, 3000);
+		});
 	});
 
 	await s.listen(s.port);
 	ee.url = `${s.url}/abort`;
+	ee.redirectUrl = `${s.url}/redirect`;
 
 	return ee;
 }
 
+test('cancel do not retry after cancelation', async t => {
+	const helper = await createAbortServer();
+
+	const p = got(helper.redirectUrl, {
+		retries: _ => {
+			t.fail('Makes a new try after cancelation');
+		}
+	});
+
+	helper.on('sentRedirect', () => {
+		p.cancel();
+	});
+
+	await t.throws(p, PCancelable.CancelError);
+	await t.notThrows(helper.aborted, 'Request finished instead of aborting.');
+});
+
 test('cancel in-progress request', async t => {
 	const helper = await createAbortServer();
 	const body = new Readable({