Skip to content
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

Conversation

EvanHahn
Copy link
Contributor

@EvanHahn EvanHahn commented Apr 30, 2019

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 EvanHahn requested review from jbbakst and kasrak April 30, 2019 18:19
@EvanHahn EvanHahn force-pushed the fix_callback_to_promise_bug_with_varying_argument_lengths branch from 850e337 to 5cbe669 Compare April 30, 2019 18:20
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 EvanHahn force-pushed the fix_callback_to_promise_bug_with_varying_argument_lengths branch from 5cbe669 to 20497fa Compare April 30, 2019 18:30
Copy link
Contributor

@kasrak kasrak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch!

@EvanHahn EvanHahn merged commit 010eb64 into master Apr 30, 2019
@EvanHahn 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants