Skip to content

Make methods return bluebird promise instead native promise#76

Open
BnayaZil wants to merge 4 commits into
benjamingr:masterfrom
BnayaZil:fix_native_promise
Open

Make methods return bluebird promise instead native promise#76
BnayaZil wants to merge 4 commits into
benjamingr:masterfrom
BnayaZil:fix_native_promise

Conversation

@BnayaZil

@BnayaZil BnayaZil commented Jun 14, 2017

Copy link
Copy Markdown
Contributor

Solve #72
#goodnessSquad

@benjamingr benjamingr left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you please write tests that assert that the promise is bluebird-api and assert the changes you make change those tests from failing to passing?

Comment thread promiseFns/call.js
})());
};

Bluebird.call = (o, ...args) => Bluebird.resolve(o).call(...args);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/any.js
@@ -1,5 +1,8 @@
module.exports = (Bluebird) => {
Bluebird.any = (prom, n) => Bluebird.resolve(prom).any();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it wasn't..

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

But Bluebird.resolve already returns a bluebird instance, so why would wrapping help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because you resolve before you do the any, you need to resolve the any values.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

But .any() already returns a bluebird promise.

Comment thread promiseFns/delay.js
Bluebird.prototype.delay = function delay(ms) {
return this.then(obj => new Bluebird((onFulfilled) => setTimeout(() => onFulfilled(obj), ms)));
}
Bluebird.delay = (ms, o) => Bluebird.resolve(o).delay(ms);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/each.js Outdated
})());
};
Bluebird.prototype.each = function each(iterator) {
return Bluebird.resolve((async () => {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Was anything actually changed here?

Comment thread promiseFns/filter.js
@@ -1,6 +1,9 @@
const util = require("./util");
module.exports = (Bluebird) => {
Bluebird.filter = (x, predicate, opts) => Bluebird.resolve(x).filter(predicate, opts);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

it wasn't..

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

But Bluebird.resolve already returns a bluebird instance, so why would wrapping help?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Because you resolve before you do the filter, you need to resolve the filter values.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

But p.filter(predicate) already returns a bluebird promise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

They are not, filter, each and map didn't return bluebird promise, I have tested it.
I can move bluebird resolve wrapper to the prototype functions if it seems cleaner to you.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Can you add a test so we can see?

Comment thread promiseFns/mapSeries.js
})());
};

Bluebird.mapSeries = (promise, iterator) => Bluebird.resolve(promise).mapSeries(iterator);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/props.js
return ret;
})());
};
Bluebird.props = o => Bluebird.resolve(o).props();

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/reduce.js
})());
};

Bluebird.reduce = (promise, reducer, initialValue) => Bluebird.resolve(promise).reduce(reducer, initialValue);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/some.js
@@ -1,5 +1,8 @@
module.exports = (Bluebird) => {
Bluebird.some = (prom, n) => Bluebird.resolve(prom).some(n);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

Comment thread promiseFns/timeout.js
return winner;
})());
}
Bluebird.timeout = (ms, rejectDesc, o) => Bluebird.resolve(o).delay(ms)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

This change seems unnecessary.

@BnayaZil

BnayaZil commented Jun 15, 2017

Copy link
Copy Markdown
Contributor Author

The setScheduler tests failed before my changes, except them, all tests are passing, I added tests for checking if the returned promise is an instance of Bluebird, I hope using instanceof it's fine, I checked it in positive and negative and it seems ok.

@BnayaZil

BnayaZil commented Jul 1, 2017

Copy link
Copy Markdown
Contributor Author

@benjamingr I add tests to make sure the promises from the prototype returns a non-bluebird instance.
As I suggested before, If the pattern is to wrap the methods with bluebird object in the prototype I can move the wrapper and get rid of the redundant test.

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