-
Notifications
You must be signed in to change notification settings - Fork 10
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
Use promises instead of callbacks #16
Conversation
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.
Looks good to me, only formatting nitpicks (and one suspected bug in fromFile
).
bin/hdt
Outdated
}); | ||
|
||
}, | ||
(error) => { |
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.
No parentheses needed (consistency)
lib/HdtDocument.cc
Outdated
@@ -54,7 +54,7 @@ const Nan::Persistent<Function>& HdtDocument::GetConstructor() { | |||
Nan::SetPrototypeMethod(constructorTemplate, "_searchTriples", SearchTriples); | |||
Nan::SetPrototypeMethod(constructorTemplate, "_searchLiterals", SearchLiterals); | |||
Nan::SetPrototypeMethod(constructorTemplate, "_searchTerms", SearchTerms); | |||
Nan::SetPrototypeMethod(constructorTemplate, "close", Close); | |||
Nan::SetPrototypeMethod(constructorTemplate, "_close", Close); |
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.
Alignment broken
lib/hdt.js
Outdated
triples, | ||
totalCount, | ||
hasExactCount, | ||
}); |
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.
No need to return; I'd write the above as
if (err)
reject(err);
else
resolve({ triples, totalCount, hasExactCount })
lib/hdt.js
Outdated
}, self); | ||
HdtDocumentPrototype.countTriples = function (subject, predicate, object) { | ||
return this.search(subject, predicate, object, { offset: 0, limit: 0 }) | ||
.then(results => ({ totalCount: results.totalCount, hasExactCount: results.hasExactCount })); |
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.
Maybe remove the then
part? Doesn't matter that there's an extra triples
field (plus, fewer object shapes for V8).
lib/hdt.js
Outdated
resolve({ | ||
literals, | ||
totalCount, | ||
}); |
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.
Idem as above, without return
and resolve
on a single line.
lib/hdt.js
Outdated
}; | ||
HdtDocumentPrototype.close = function () { | ||
return new Promise((resolve, reject) => { | ||
this._close(function (e) { |
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.
err
for consistency.
lib/hdt.js
Outdated
case 'Non-HDT Section': | ||
return reject(new Error('The file "' + filename + '" is not a valid HDT file')); | ||
default: | ||
reject(error); |
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.
return
missing?
And if so, how did the unit tests miss this?
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.
A reject
handler from a Promise constructor does not need to be returned, it only needs to be called. Added the return here for consistency
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.
It's not about returning a value, but rather about terminating the function right there. Otherwise, execution continues, and that's not what you want. It's not observable in this particular case though, but we reject because we don't want execution to continue.
test/hdt-test.js
Outdated
hdt.fromFile(null, function (error) { | ||
this.should.equal(self); | ||
it('should throw an error', function () { | ||
return hdt.fromFile(null).then(() => Promise.reject(new Error('Expected an error')), (error) => { |
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.
Throughout: no parens around error
for consistency.
test/hdt-test.js
Outdated
}); | ||
it('should throw an error', function () { | ||
return document.searchTriples(null, null, null).then( | ||
() => Promise.reject(new Error('Expected an error')), |
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.
() =>
on line above for consistency.
test/hdt-test.js
Outdated
}); | ||
it('should throw an error', function () { | ||
return document.searchLiterals('abc').then( | ||
() => Promise.reject(new Error('Expected an error')), |
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.
Idem.
fixed the style issues per your latest comments |
Thanks for fixing! Given that this will require a new semver major, any other breaking interface changes we might want to consider? Also, README will still need to be updated. |
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.
Code is perfect. Should still update README to match the code.
Discussed with @LaurensRietveld that he will add a TypeScript definition for the new interface to this PR. After that, I'll merge and release. |
@LaurensRietveld Perfect, thanks! |
@RubenVerborgh : would you mind if we merge this to develop? Then we can create a new PR for #17 without having to rebase after. |
It's assigned to me, I'll do it today.
|
b6828e2
to
f39bc59
Compare
I just shortened some commit messages to stay below 50 characters, as long commit titles might lead to tooling problems later on. |
f39bc59
to
3341474
Compare
Released as v2.0.0. |
What changed:
triples
,totalCount
,hasExactCount
). As promises should return only one value, I wrapped these in an object.var
keywords toconst
if applicableself
reference that were bound to the callbacks. Don't believe there is much need for them anymore with arrow functions, promises, and (on more recent versions of nodejs) async/await.I planned on transforming the
prototype
functions to a regular es6 class, but that won't do us any good as we shouldnt create our own class, but simply extend the native class by assiging extra functions to its prototype.