Skip to content

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

Merged
megawac merged 2 commits intocaolan:masterfrom
raydog:master
Jun 23, 2016
Merged

Callback can be called twice in asyncify when using promises.#1197
megawac merged 2 commits intocaolan:masterfrom
raydog:master

Conversation

@raydog
Copy link
Copy Markdown
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
Copy Markdown
Collaborator

megawac commented Jun 23, 2016

Good catch, 100% agree this is the correct behaviour

Comment thread mocha_test/asyncify.js
// normal cases, but not here, since we expect unhandled rejections:
process.on('unhandledRejection', function () {
// Ignore.
});
Copy link
Copy Markdown
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.

2 participants