gpt4 book ai didi

javascript - 为什么在我的测试中使用 .then() 比 await 快 3 倍?

转载 作者:行者123 更新时间:2023-11-30 14:31:35 25 4
gpt4 key购买 nike

这完全可能是我误解了 Javascript 中 async/await 的工作方式,但我还没有弄明白。

我有一个简单的测试,它使用一些助手在实际运行测试之前生成模拟数据。

这是辅助函数:

async create(numCustomers, businessId, options) {

options = _.assign({ type: 'customer' }, options);

const customers = await Factory.createMany('user', options, numCustomers);

return Promise.all(customers.map(async (c) => {
await AccountHelper.create(businessId, c.get('id'));
return c;
}));
}

这是测试的两个版本:

async/await 版本:

const customers = await CustomerHelper.create(10, Constants.fakeBusinessId);

await Promise.all(customers.map(async (c) => {
await PetHelper.create(1, c.get('id'));
}));

const res = await server.inject(request);
expect(res.statusCode).to.equal(200);

.then() 版本:

CustomerHelper.create(10, Constants.fakeBusinessId).then(async (customers) => {

await Promise.all(customers.map(async (c) => {
await PetHelper.create(1, c.get('id'));
}));

const res = await server.inject(request);
expect(res.statusCode).to.equal(200);
});

.then() 版本在大约 2 秒内完成,而 async/await 版本在将近 7 秒内完成。在这两种形式之间转换似乎是唯一的变量。

我在 OSX 上运行 Node 8.9.4。

我感谢任何见解或教育:)

最佳答案

这两种方法的持续时间应该大致相同,只要您正确地向测试框架发出测试已完成的信号。 (在大多数测试框架中,通过返回 promise 或调用 done 回调)

如果您没有正确地向框架发出信号,那么测试将在异步处理完成(甚至在许多情况下甚至开始)之前退出。此外,如果您的断言/期望等待异步事件,那么它们甚至不会被执行,并且没有断言的测试(在大多数框架中)是通过测试。正因为如此, async/await 正在成为测试的首选样式,因为它更难(尽管并非不可能)错误地向测试框架发出信号(如您的示例所示) .

向测试框架发出测试完成信号的正确方法(对于 mocha/jest 风格的框架):

it('does async work with await', async function (){
let result = await asyncWork()
assertItWorked(result)
})

it('does async work with returned promise', function (){
return asyncWork().then(function (result) {
assertItWorked(result)
})
})

it('does async work with promise and callback', function (done){
asyncWork().then(function (result) {
assertItWorked(result)
done() // note that this method will timeout if it fails, instead of displaying the error message, but you still get a failure
})
})

it('does async work with promise and chained callback', function (done){
asyncWork().then(function (result) {
assertItWorked(result)
}).then(done)
})

看似正确但不正确的方法:

it('does async work but does not wait', function (done){
asyncWork().then(function (result) {
assertItWorked(result)
})
done() // this gets called before async work is done, and no assertions are made
})

it('does async work with promise and chained callback, but calls callback too soon', function (done){
asyncWork().then(function (result) {
assertItWorked(result)
}).then(done()) // this is effectively identical to the previous example, because done gets called immediately
})

还请注意,async/await 样式完全捕获了 asyncWork 抛出的错误/拒绝,尽管使用更多代码其他样式可以作为好吧。

关于javascript - 为什么在我的测试中使用 .then() 比 await 快 3 倍?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51092670/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com