[英]js await throw TypeError
I make a simple sleep promise function: 我做了一个简单的睡眠承诺函数:
let sleep = function(time) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, time);
})
}
then use await/async in karma
with chai
然后在与
chai
karma
使用等待/异步
var test = async function() {
// works
await sleep(DELAY)
let secondItem = vm.$el.querySelectorAll('.el-table__row')[1]
let secondItemTds = secondItem.querySelectorAll('td')
// throw exception
await sleep(DELAY)
secondItemTds[0].click()
expect(rowClickCnt).equal(0)
// if I CATCH the above exception, here throw exception again
await sleep(DELAY)
secondItem[5].click()
expect(rowClickCnt).equal(1)
}
test()
The code can work util the second await. 该代码可以在第二次等待中工作。
'Unhandled promise rejection', TypeError{stack: '_callee$@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:53913:30
tryCatch@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14058:44
invoke@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14296:30
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14110:28
step@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51395:30
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51406:17
run@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:50996:29
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51009:31
flush@http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:50498:11', line: 53913, sourceURL: 'http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59'}
A TypeError is thrown, but as you can see, the sleep
function never reject the Promise
. 抛出TypeError,但是正如您所看到的,
sleep
函数从不拒绝Promise
。 Another point is that the first await doesn't throw Exception. 另一点是,第一次等待不会引发Exception。 Anyone can help me out?
有人可以帮我吗?
Babel config: Babel配置:
{
"presets": [
["env", { "modules": false }],
"stage-2"
],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": [ "istanbul" ]
}
}
}
I have had the same problem. 我曾经也有过一样的问题。
Adding support of async await to babel helps me. 为babel添加async await支持对我有帮助。
npm install --save-dev babel-preset-es2017
And add this preset to babel options: 并将此预设添加到babel选项:
"presets": [ <your presets> , "es2017"]
Try it please. 请尝试一下。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.