我做了一个简单的睡眠承诺功能:
let sleep = function(time) {
return new Promise(function (resolve) {
setTimeout(function () {
resolve();
}, time);
})
}
然后在karma
chai
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()
代码可以在第二个等待中使用。
'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'}
抛出TypeError,但正如您所看到的,sleep
函数从不拒绝Promise
。另一点是,第一个等待不会抛出异常。任何人都可以帮助我吗?
Babel config:
{
"presets": [
["env", { "modules": false }],
"stage-2"
],
"plugins": ["transform-runtime"],
"comments": false,
"env": {
"test": {
"presets": ["env", "stage-2"],
"plugins": [ "istanbul" ]
}
}
}
答案 0 :(得分:0)
我遇到了同样的问题。
为babel添加async await支持可以帮助我。
npm install --save-dev babel-preset-es2017
并将此预设添加到babel选项:
"presets": [ <your presets> , "es2017"]
请试试。