从异步函数

时间:2018-05-26 20:53:27

标签: javascript error-handling async-await

如果用户证明脚本中出现错误,我想显示错误消息。例如:

function one() { consolef.log("async"); }
async function two() { consolef.log("async"); }

one();
await two();

window.onerror仅在one()上触发,但不在two()上触发:

window.onerror = function(msg, url, lineNo, columnNo, error) {
  var string = msg.toLowerCase();
  if (string.indexOf(substring) > -1) {
    alert('Script Error: See Browser Console for Detail');
  } else {
    console.log('Error showed in browser view');
    document.body.innerHTML += ` <div class = "alert alert-danger" >` + error + ` < /a>. </div>`;
  }

  return false;
};

这两个错误都显示在控制台内,但在浏览器中只显示one()

编辑: 我被指向12。但两者都建议使用onrejectionhandledonunhandledrejection,但Firefox不支持这两种功能。所以我不能为所有用户使用这些功能。

0 个答案:

没有答案
相关问题