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

Callback can be called twice in asyncify when using promises. #1197

Merged
merged 2 commits into from
Jun 23, 2016

Conversation

raydog
Copy link
Contributor

@raydog raydog commented Jun 23, 2016

Code to reproduce the bug:

const async = require('async');

const i_return_a_promise = () => Promise.resolve("Hello");

const fn = async.asyncify(i_return_a_promise);

var _count = 0;
fn((err) => {
  console.log("CB CALL #%d", ++_count, err);
  throw new Error("D:");
});

This code will print out:

CB CALL #1 null
CB CALL #2 [Error: D:]

This is because .then on a promise will catch errors, and return a rejected promise, which would then be caught by the .catch code, calling the callback twice. This PR fixes that behavior by handling both the resolve case and the rejection case in the .then handler.

@megawac megawac added the bug label Jun 23, 2016
@megawac megawac merged commit e739104 into caolan:master Jun 23, 2016
@megawac
Copy link
Collaborator

megawac commented Jun 23, 2016

Good catch, 100% agree this is the correct behaviour

// normal cases, but not here, since we expect unhandled rejections:
process.on('unhandledRejection', function () {
// Ignore.
});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just realized, can you remove this after the test completes? process.off or only bind it with process.once

Please make a followup pr

suguru03 added a commit to suguru03/neo-async that referenced this pull request Jun 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants