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
support promise return in ospec #1928
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,40 +84,56 @@ module.exports = new function init(name) { | |
if (cursor === fns.length) return | ||
|
||
var fn = fns[cursor++] | ||
var timeout = 0, delay = 200, s = new Date | ||
var isDone = false | ||
|
||
function done(err) { | ||
if (err) { | ||
if (err.message) record(err.message, err) | ||
else record(err) | ||
subjects.pop() | ||
next() | ||
} | ||
if (timeout !== undefined) { | ||
timeout = clearTimeout(timeout) | ||
if (delay !== Infinity) record(null) | ||
if (!isDone) next() | ||
else throw new Error("`" + arg + "()` should only be called once") | ||
isDone = true | ||
} | ||
else console.log("# elapsed: " + Math.round(new Date - s) + "ms, expected under " + delay + "ms") | ||
} | ||
|
||
function startTimer() { | ||
timeout = setTimeout(function() { | ||
timeout = undefined | ||
record("async test timed out") | ||
next() | ||
}, Math.min(delay, 2147483647)) | ||
} | ||
|
||
if (fn.length > 0) { | ||
var timeout = 0, delay = 200, s = new Date | ||
var isDone = false | ||
var body = fn.toString() | ||
var arg = (body.match(/\(([\w$]+)/) || body.match(/([\w$]+)\s*=>/) || []).pop() | ||
if (body.indexOf(arg) === body.lastIndexOf(arg)) throw new Error("`" + arg + "()` should be called at least once") | ||
try { | ||
fn(function done() { | ||
if (timeout !== undefined) { | ||
timeout = clearTimeout(timeout) | ||
if (delay !== Infinity) record(null) | ||
if (!isDone) next() | ||
else throw new Error("`" + arg + "()` should only be called once") | ||
isDone = true | ||
} | ||
else console.log("# elapsed: " + Math.round(new Date - s) + "ms, expected under " + delay + "ms") | ||
}, function(t) {delay = t}) | ||
fn(done, function(t) {delay = t}) | ||
} | ||
catch (e) { | ||
record(e.message, e) | ||
subjects.pop() | ||
next() | ||
done(e) | ||
} | ||
if (timeout === 0) { | ||
timeout = setTimeout(function() { | ||
timeout = undefined | ||
record("async test timed out") | ||
next() | ||
}, Math.min(delay, 2147483647)) | ||
startTimer() | ||
} | ||
} | ||
else { | ||
fn() | ||
nextTickish(next) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IIRC there was a good reason to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. readd |
||
var p = fn() | ||
if (p && p.then) { | ||
startTimer() | ||
p.then(function() { done() }, done) | ||
} else { | ||
nextTickish(next) | ||
} | ||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,7 +131,7 @@ o.spec("promise", function() { | |
callAsync(function() {resolve(promise)}) | ||
}) | ||
|
||
promise.then(null, done) | ||
promise.then(null, () => done()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My one and only nit: use ES5-style functions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @isiahmeadows only here or everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For this, only in places where you have a diff. The rest should be flagged in a separate issue, if you'd like to file one for that. (The polyfill tests should be runnable in an ES5 environment for obvious reasons.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mean, I have some other arrows in this PR. Should I convert them too? I assume yes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, IMO tests are fine to use arrows but library has to use ES5 funcs for compat w/o transpilation. This sounds like a good danger rule... 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I converted one arrow fn in ospec to a fn-expression. Left the tests untouched. |
||
}) | ||
o("non-function onFulfilled is ignored", function(done) { | ||
var promise = Promise.resolve(1) | ||
|
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.
Danger prevents the test suite from running, but I wonder how much time it takes to create these closures unconditionally?
if (fn.length > 0) {
could becomevar res; if (fn.length > 0 || (res = fn(), res != null && typeof res.then === 'function;)) {
And in that if block, assign
done
to a variable and, either callfn(done)
orres.then(done)
...If it doesn't make sense don't worry, I'll have to read this with a fresh mind :-)