简体   繁体   English

js等待抛出TypeError

[英]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.

相关问题 带有等待功能循环的抛出错误 - Throw Error with loop of await function Async / Await在Mocha中抛出一个错误 - Async/Await throw an error in Mocha NodeJS抛出新的TypeError - NodeJS throw new TypeError node.js和PostgreSQL查询-抛出新的TypeError(&#39;第一个参数必须是字符串或Buffer&#39;); - node.js and PostgreSQL query - throw new TypeError('first argument must be a string or Buffer'); 运行gulp会导致“ path.js:7抛出新的TypeError(&#39;路径必须是字符串。已接收&#39;+ inspect(path));” - Running gulp gives “path.js:7 throw new TypeError('Path must be a string. Received ' + inspect(path));” 我得到一个fs.js:347 throw new TypeError(&#39;Bad arguments&#39;); - I'm getting a fs.js:347 throw new TypeError('Bad arguments'); 当我想访问对象详细信息时,React JS引发“ TypeError:无法读取未定义的属性&#39;名称&#39;”错误 - React JS throw “TypeError: Cannot read property 'name' of undefined” error when i want to access object details 为什么 await eval('`${await foo("xxx")}`') 会抛出错误 - Why does await eval('`${await foo("xxx")}`') throw an error Electron和Babel 6 async / await抛出意外的令牌 - Electron and Babel 6 async / await throw unexpected token 在一个表达式中结合 throw new Error 和 await - Combine throw new Error with await in one expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM