为什么TypeScript转译后,自行帮我处理了async方法中的异常?
一段包含async方法的ts代码: 转译后,会生成包含下面代码片段的es5代码:
var __extends = this && this.__extends || function () {
var _extendStatics = function extendStatics(d, b) {
_extendStatics = __WEBPACK_IMPORTED_MODULE_7_babel_runtime_core_js_object_set_prototype_of___default.a || { __proto__: [] } instanceof Array && function (d, b) {
d.__proto__ = b;
} || function (d, b) {
for (var p in b) {
if (b.hasOwnProperty(p)) d[p] = b[p];
}
};
return _extendStatics(d, b);
};
return function (d, b) {
_extendStatics(d, b);
function __() {
this.constructor = d;
}
d.prototype = b === null ? __WEBPACK_IMPORTED_MODULE_6_babel_runtime_core_js_object_create___default()(b) : (__.prototype = b.prototype, new __());
};
}();
var __decorate = this && this.__decorate || function (decorators, target, key, desc) {
var c = arguments.length,
r = c < 3 ? target : desc === null ? desc = __WEBPACK_IMPORTED_MODULE_5_babel_runtime_core_js_object_get_own_property_descriptor___default()(target, key) : desc,
d;
if ((typeof Reflect === "undefined" ? "undefined" : __WEBPACK_IMPORTED_MODULE_4_babel_runtime_helpers_typeof___default()(Reflect)) === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);else for (var i = decorators.length - 1; i >= 0; i--) {
if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
}return c > 3 && r && __WEBPACK_IMPORTED_MODULE_3_babel_runtime_core_js_object_define_property___default()(target, key, r), r;
};
var __awaiter = this && this.__awaiter || function (thisArg, _arguments, P, generator) {
return new (P || (P = __WEBPACK_IMPORTED_MODULE_2_babel_runtime_core_js_promise___default.a))(function (resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e) {
reject(e);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e) {
reject(e);
}
}
function step(result) {
result.done ? resolve(result.value) : new P(function (resolve) {
resolve(result.value);
}).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = this && this.__generator || function (thisArg, body) {
var _ = { label: 0, sent: function sent() {
if (t[0] & 1) throw t[1];return t[1];
}, trys: [], ops: [] },
f,
y,
t,
g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof __WEBPACK_IMPORTED_MODULE_1_babel_runtime_core_js_symbol___default.a === "function" && (g[__WEBPACK_IMPORTED_MODULE_0_babel_runtime_core_js_symbol_iterator___default.a] = function () {
return this;
}), g;
function verb(n) {
return function (v) {
return step([n, v]);
};
}
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) {
try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0:case 1:
t = op;break;
case 4:
_.label++;return { value: op[1], done: false };
case 5:
_.label++;y = op[1];op = [0];continue;
case 7:
op = _.ops.pop();_.trys.pop();continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
_ = 0;continue;
}
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
_.label = op[1];break;
}
if (op[0] === 6 && _.label < t[1]) {
_.label = t[1];t = op;break;
}
if (t && _.label < t[2]) {
_.label = t[2];_.ops.push(op);break;
}
if (t[2]) _.ops.pop();
_.trys.pop();continue;
}
op = body.call(thisArg, _);
} catch (e) {
op = [6, e];y = 0;
} finally {
f = t = 0;
}
}if (op[0] & 5) throw op[1];return { value: op[0] ? op[1] : void 0, done: true };
}
};
其中:
catch (e) {
op = [6, e];y = 0;
}
这段是什么意思,我今天调试发现我async方法中其实报了个空指针,但控制台没有任何迹象,调试后才发现被抓了,而且没有任何处理,这是ts的什么配置来控制的吗?感觉这样不合理吧.
2 回复
下面这段会继续抛出吧
if (op[0] & 5) throw op[1]
@waitingsong 都已经catch了,里面又没继续抛, 不过貌似也不是全部异常都会走到里面那个catch. 也有的异常会在控制台显示出来.