Javascript done() 回调的重点是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37646949/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the point of the done() callback?
提问by ThePumpkinMaster
In Mochajs, they use done()
to test for asynchronous code, like so:
在 Mochajs 中,它们用于done()
测试异步代码,如下所示:
describe('User', function() {
describe('#save()', function() {
it('should save without error', function(done) {
var user = new User('Luna');
user.save(function(err) {
if (err) throw err;
done();
});
});
});
});
What does this mean exactly? I did console.log(done.toString())
and I got this:
这究竟是什么意思?我做到了console.log(done.toString())
,我得到了这个:
function (err) {
if (err instanceof Error || toString.call(err) === '[object Error]') {
return done(err);
}
if (err) {
if (Object.prototype.toString.call(err) === '[object Object]') {
return done(new Error('done() invoked with non-Error: '
+ JSON.stringify(err)));
}
return done(new Error('done() invoked with non-Error: ' + err));
}
done();
}
Is the done()
at the very end here different than the done()
in the first piece of code?
是done()
在最后这里比的不同done()
在第一段代码?
采纳答案by Louis
Mocha is able to handle synchronous and asynchronous tests. When you run a synchronous test, you can just pass it as an anonymous function to it
and you don't have to do anything else: Mocha knows the test is over when the function returns. However, if you are running an asynchronous test, you have to tell Mocha that the test is asynchronous.There are two ways to do this:
Mocha 能够处理同步和异步测试。当你运行一个同步测试时,你可以将它作为一个匿名函数传递给it
,你不需要做任何其他事情:当函数返回时,Mocha 知道测试已经结束。但是,如果您正在运行异步测试,则必须告诉 Mocha 该测试是异步的。有两种方法可以做到这一点:
Declare that the anonymous function you pass to
it
takes a parameter. Mocha will call your anonymous function with a single parameter which is a function you must call to indicate that your test is over. (This parameter is calleddone
due to tradition. You could call itcomplete
,cb
orplatypus
and it would work just the same.) If you calldone
without a value, the test is successful. With a value, the test is a failure and the value should be anError
object or an object derived fromError
.Return a promise: Mocha will wait for the promise to be resolved or rejected. If resolved, the test is successful. If rejected, the test failed.
声明您传递给的匿名函数
it
接受一个参数。Mocha 将使用单个参数调用您的匿名函数,该函数是您必须调用以指示您的测试结束的函数。(done
由于传统而调用此参数。您可以调用它complete
,cb
或者platypus
它会起作用。)如果您done
不带值调用,则测试成功。带有值,测试失败,值应该是一个Error
对象或派生自 的对象Error
。Return a promise:Mocha 将等待 promise 被解决或拒绝。如果解决,则测试成功。如果被拒绝,则测试失败。
The code you see when you do done.toString()
is just the code of the function that Mocha passes to your test when you declare it to take a parameter. You can see in it some of what I mentioned above (e.g. if you pass a parameter to done
it should be an Error
or derived from Error
). The done
in there is another done
function which is private to Mocha.
当您执行此操作时,您看到done.toString()
的代码只是 Mocha 在声明它接受参数时传递给您的测试的函数的代码。您可以在其中看到我上面提到的一些内容(例如,如果您向done
它传递参数应该是 anError
或派生自Error
)。该done
中还有另外一个done
功能是私有的摩卡。
回答by Alongkorn Chetasumon
All test cases including before()
, after()
, beforeEach()
, afterEach()
must call done()
at the end to tell Mocha that all tasks are completed.
所有测试用例包括before()
, after()
, beforeEach()
,afterEach()
必须done()
在最后调用以告诉 Mocha 所有任务都已完成。
If done()
is missing, a timeout exception will be raised because Mocha will wait for done()
until timeout.
如果done()
缺少,则会引发超时异常,因为 Mocha 将等待done()
直到超时。
回答by libik
Because of asynchronous nature of node.js, you have to tell to Mocha that your finished test.
由于 node.js 的异步特性,你必须告诉 Mocha 你完成了测试。
For classic synchronous languages, you are done when the method has finished. But in node, first the whole method is executed and then, some time after is executed inner body of user.save()
.
对于经典的同步语言,当方法完成时你就完成了。但是在 node 中,首先执行整个方法,然后在user.save()
.
The Mocha just waits with test until done()
, is called, because it does not have any other option to find if something else should be executed or it is finished.
Mocha 只是等待 test 直到done()
, 被调用,因为它没有任何其他选项来确定是否应该执行其他操作或完成。
The output you have is just body of function done
.
您拥有的输出只是函数体done
。