function doAsyncOp() { // the next line will kill execution throw new Error("something is bad"); return new Promise(function(resolve, reject) { if (somethingIsBad) { throw new Error("something is bad"); } resolve("nothing is bad"); }); } // assume `doAsyncOp` does not have the killing error function x() { var val = doAsyncOp().then(function() { // this one will work just fine throw new Error("I just think an error should be here"); }); // this one will kill execution throw new Error("The more errors, the merrier"); return val; } 在 async 函数的 Promise 中抛掉足误就不会产生有关范围的问题——你可以在 async 函数中随时随地抛掉足误,它总会被 Promise 抓住:
- async function doAsyncOp() {
-
- // the next line is fine
-
- throw new Error("something is bad");
-
- if (somethingIsBad) {
-
- // this one is good too
-
- throw new Error("something is bad");
-
- }
-
- return "nothing is bad";
-
- }
-
-
-
- // assume `doAsyncOp` does
推荐阅读
一文看懂数据可视化:从编程工具到可视化表现方式
说到可视化,就不得不说一下大年夜数据,毕竟可视化是解决大年夜数据的一种高效的手段,而如今人人都在谈论大年夜数据,大年夜数据 ≠ 稀有据 ≠ 数据量大年夜, 离谱的是,如今就连>>>详细阅读
本文标题:用Async函数简化异步代码
地址:http://www.17bianji.com/lsqh/34848.html
1/2 1