-
Notifications
You must be signed in to change notification settings - Fork 147
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
Added support for .toPromise(PromiseConstructor) #628
Conversation
lib/index.js
Outdated
* }); | ||
*/ | ||
|
||
addMethod('toPromise', function (Promise) { |
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.
Can you name the parameter PromiseCtor
or something similar to highlight the fact that we do not use the global Promise
object.
lib/index.js
Outdated
@@ -1992,6 +1992,47 @@ addMethod('toCallback', function (cb) { | |||
}); | |||
|
|||
/** | |||
* Returns the result of a stream to a nodejs-style callback function. |
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.
Update docs.
You can just say the stream must emit at most one value or error and that the promise will resolve to that value when it is emitted, then link to [toCallback](#toCallback)
for details.
I would factor out the function toCallbackHandler(transformName, cb) {
var value;
var hasValue = false; // In case an emitted value === null or === undefined.
var hasValue = false; // In case an emitted value === null or === undefined.
return function (err, x, push, next) {
...
throws new Error(transformName + ' called on stream emitting multiple values');
...
};
}
addMethod('toCallback', function (cb) {
this.consume(toCallbackHandler('toCallback', cb)).resume();
});
addMethod('toPromise', function (PromiseCtor) {
return new PromiseCtor(function(resolve, reject) {
this.consume(toCallbackHandler('toPromise', function (err, x) {
...
})).resume();
});
});
The tests themselves look fine, but can you organize them into suites instead (e.g., like exports.toPromise = {
ArrayStream: function ...
'returns error for streams with multiple values': function ...
...
};
Yep! I would love that; similarly with |
What if PromiseCtor was optional and defaulted to global Promise? Best of both worlds? |
I had considered making
@Ginden, sorry I haven't responded to your latest changes. I don't get an email notification when you update the commits in your PR, so you have to make a comment. Are you planning on placing the tests in a separate |
I do not plan it. |
Can you remove the file from the PR then? |
@vqvu Done. |
Thanks! |
I'm not sure about error message handling and tests.
Did you consider splitting
test/test.js
to many files?