2017-06-22 127 views
0

我做一个简单的睡眠承诺功能:JS等待抛出类型错误

let sleep = function(time) { 
    return new Promise(function (resolve) { 
    setTimeout(function() { 
     resolve(); 
    }, time); 
    }) 
} 

再使用的await /异步在karmachai

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() 

的代码可以UTIL第二的await工作。

'Unhandled promise rejection', TypeError{stack: '[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:53913:30 
[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14058:44 
[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14296:30 
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:14110:28 
[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51395:30 
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51406:17 
[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:50996:29 
http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:51009:31 
[email protected]://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59:50498:11', line: 53913, sourceURL: 'http://localhost:9876/base/index.js?312a09ec6fd4038632c2250d472217bc6ee69c59'} 

一个类型错误被抛出,但你可以看到,sleep函数从不拒绝Promise。还有一点是,第一个等待不会抛出异常。任何人都可以帮助我?

通天配置:

{ 
    "presets": [ 
    ["env", { "modules": false }], 
    "stage-2" 
    ], 
    "plugins": ["transform-runtime"], 
    "comments": false, 
    "env": { 
    "test": { 
     "presets": ["env", "stage-2"], 
     "plugins": [ "istanbul" ] 
    } 
    } 
} 
+0

你怎么知道这是'等待睡眠(...)'线后抛出的代码? – Bergi

+0

....你是对的,业务代码抛出这个异常,而不是睡觉。 – Leon

回答

0

我有同样的问题。

向babel添加对异步等待的支持可以帮助我。

npm install --save-dev babel-preset-es2017 

而这个预设添加到通天选项:

"presets": [ <your presets> , "es2017"] 

试试看吧。