diff --git a/lib/run_action.js b/lib/run_action.js index 5403f9fb..90ac85ce 100644 --- a/lib/run_action.js +++ b/lib/run_action.js @@ -35,16 +35,17 @@ function runAction(base, method, path, queryParams, bodyData, callback, numAttem headers['User-Agent'] = userAgent; } + var controller = new AbortController(); var options = { method: method.toUpperCase(), headers: headers, + signal: controller.signal, }; if (bodyData !== null) { options.body = JSON.stringify(bodyData); } - var controller = new AbortController(); var timeout = setTimeout(function() { controller.abort(); }, base._airtable.requestTimeout); diff --git a/test/find.test.js b/test/find.test.js index 706286ac..4ebdc011 100644 --- a/test/find.test.js +++ b/test/find.test.js @@ -146,4 +146,28 @@ describe('record retrival', function() { expect(foundRecord.get('Name')).toBe('Rebecca'); }); }); + + it('can timeout if the servers are slow', function(done) { + var recordId = 'record1'; + + testExpressApp.set('handler override', function(req) { + expect(req.method).toBe('GET'); + expect(req.url).toBe('/v0/app123/Table/record1?'); + // Timeout before returning a response + }); + + return airtable + .base('app123') + .table('Table') + .find(recordId) + .then( + function() { + throw new Error('Promise unexpectedly fufilled.'); + }, + function(err) { + expect(err.message).toMatch(/aborted/); + done(); + } + ); + }); }); diff --git a/test/test_helpers.js b/test/test_helpers.js index 8933b3e6..4396ab1d 100644 --- a/test/test_helpers.js +++ b/test/test_helpers.js @@ -143,6 +143,7 @@ function getMockEnvironmentAsync(options) { airtable: new Airtable({ apiKey: 'key123', endpointUrl: 'http://localhost:' + testServerPort, + requestTimeout: 100, }), teardownAsync: util.promisify(testServer.close.bind(testServer)), testExpressApp: app,