-
Notifications
You must be signed in to change notification settings - Fork 407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix bug in callback-to-promise with varying argument lengths #103
Merged
EvanHahn
merged 1 commit into
master
from
fix_callback_to_promise_bug_with_varying_argument_lengths
Apr 30, 2019
Merged
Fix bug in callback-to-promise with varying argument lengths #103
EvanHahn
merged 1 commit into
master
from
fix_callback_to_promise_bug_with_varying_argument_lengths
Apr 30, 2019
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EvanHahn
force-pushed
the
fix_callback_to_promise_bug_with_varying_argument_lengths
branch
from
April 30, 2019 18:20
850e337
to
5cbe669
Compare
EvanHahn
pushed a commit
that referenced
this pull request
Apr 30, 2019
This adds partial tests for the `Record` class. This is a test-only change. `npm test` passes. (I was investigating the underlying issue behind [#103][1] and thought the `Record` class was the culprit at first. That turned out to be wrong, but these tests are a useful byproduct of that mistake.) [1]: #103
This was a latent bug that was exposed after some testing, but is not new. We use an internal function, `callbackToPromise`, to allow functions to be in "callback mode" or "promise mode": myTable.create({foo: 'boo'}, (err, record) => { // ... }); const record = myTable.create({foo: 'boo'}); When calling `callbackToPromise`, you can specify an optional `callbackArgIndex`. If it's unspecified (which it usually is), the callback index is assumed to be the last argument. However, it had a bug. The logic I stated above, "the callback index is assumed to be the last argument", wasn't done every time, but only the _first_ time. That meant that something like this would fail: function sum(a, b, maybeC, callback) { let c; if (callback === undefined) { callback = maybeC; c = 0; } else { c = maybeC; } callback(null, a + b + c); } const wrapped = callbackToPromise(sum, null); // In the following call, the callback arg index is the 3rd argument, // which is correct. wrapped(1, 2, () => { // In the following call, the callback arg index is STILL the 3rd // argument, which is 5--that's incorrect. This would then return // a Promise, which is unexpected, and never call the callback. wrapped(3, 4, 5, () => { // This is never called! }); }); This wasn't just theoretical. This hangs forever on `[email protected]` (make sure to fill in real values): const myTable = new Airtable({apiKey: 'key123'}) .base('app123') .table('My Table'); myTable.create({foo: 'bar'}, (err1) => { if (err1) { throw err1; } myTable.create({foo: 'baz'}, {typecast: true}, (err2) => { // The record is created but this callback is never called // because the second call to `myTable.create` returned a // Promise. }); }); I've made sure to add tests for `callbackToPromise` as part of this commit.
EvanHahn
force-pushed
the
fix_callback_to_promise_bug_with_varying_argument_lengths
branch
from
April 30, 2019 18:30
5cbe669
to
20497fa
Compare
kasrak
reviewed
Apr 30, 2019
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch!
kasrak
approved these changes
Apr 30, 2019
EvanHahn
deleted the
fix_callback_to_promise_bug_with_varying_argument_lengths
branch
April 30, 2019 18:43
EvanHahn
pushed a commit
that referenced
this pull request
Jul 8, 2019
This adds partial tests for the `Record` class. This is a test-only change. `npm test` passes. (I was investigating the underlying issue behind [#103][1] and thought the `Record` class was the culprit at first. That turned out to be wrong, but these tests are a useful byproduct of that mistake.) [1]: #103
PeterPan627
added a commit
to PeterPan627/airtable.js
that referenced
this pull request
May 28, 2023
This adds partial tests for the `Record` class. This is a test-only change. `npm test` passes. (I was investigating the underlying issue behind [#103][1] and thought the `Record` class was the culprit at first. That turned out to be wrong, but these tests are a useful byproduct of that mistake.) [1]: Airtable/airtable.js#103
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This was a latent bug that was exposed after some testing, but is not new.
We use an internal function,
callbackToPromise
, to allow functions to be in "callback mode" or "promise mode":When calling
callbackToPromise
, you can specify an optionalcallbackArgIndex
. If it's unspecified (which it usually is), the callback index is assumed to be the last argument.However, it had a bug. The logic I stated above, "the callback index is assumed to be the last argument", wasn't done every time, but only the first time. That meant that something like this would fail:
This wasn't just theoretical. This hangs forever on
[email protected]
(make sure to fill in real values):I've made sure to add tests for
callbackToPromise
as part of this commit.