使用 polyfills 的简易方法 http://blog.oneapm.com/apm-tech/613.html Polyfills是一种保证使用现代代码的绝佳办法,同时还能支持旧版浏览器。但是目前polyfills使用起来很困难,因此我们开发了一种全新的服务以便简化其使用方法。
Promise对象 http://javascript.ruanyifeng.com/advanced/promise.html
正常的流程:处理Promise后,一直处理then方法,知道完成所有then。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
var jsonPromise = new Promise(function(resolve, reject) { console.log("开始处理 Promise"); resolve("正常返回-->"); //reject("异常返回-->"); }); jsonPromise.then(function(data) { // This never happens: console.log("正常1,传入", data); return '正常1'; }).catch(function (error) { console.log("拒绝1,传入", error); return '拒绝1'; }).then(function(data) { // This never happens: console.log("正常2,传入", data); return '正常2'; }).catch(function (error) { console.log("拒绝2,传入", error); return '拒绝2'; }).then(function(data) { // This never happens: console.log("正常3,传入", data); return '正常3'; }).catch(function (error) { console.log("拒绝3,传入", error); return '拒绝3'; }).then(function(data) { // This never happens: console.log("正常4,传入", data); return '正常4'; }).catch(function (error) { console.log("拒绝4,传入", error); return '拒绝4'; }).then(function(data) { // This never happens: console.log("正常5,传入", data); return '正常5'; }).catch(function (error) { console.log("拒绝5,传入", error); return '拒绝5'; }).then(function(data) { // This never happens: console.log("正常6,传入", data); return '正常6'; }).catch(function (error) { console.log("拒绝6,传入", error); return '拒绝6'; }); //输出: 开始处理 Promise 正常1,传入 正常返回--> 正常2,传入 正常1 正常3,传入 正常2 正常4,传入 正常3 正常5,传入 正常4 正常6,传入 正常5 |
异常的流程:执行Promise后,调用第一个catch,然后继续执行第一个catch后面的then。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
var jsonPromise = new Promise(function(resolve, reject) { console.log("开始处理 Promise"); //resolve("正常返回-->"); reject("异常返回-->"); }); jsonPromise.then(function(data) { // This never happens: console.log("正常1,传入", data); return '正常1'; }).catch(function (error) { console.log("拒绝1,传入", error); return '拒绝1'; }).then(function(data) { // This never happens: console.log("正常2,传入", data); return '正常2'; }).catch(function (error) { console.log("拒绝2,传入", error); return '拒绝2'; }).then(function(data) { // This never happens: console.log("正常3,传入", data); return '正常3'; }).catch(function (error) { console.log("拒绝3,传入", error); return '拒绝3'; }).then(function(data) { // This never happens: console.log("正常4,传入", data); return '正常4'; }).catch(function (error) { console.log("拒绝4,传入", error); return '拒绝4'; }).then(function(data) { // This never happens: console.log("正常5,传入", data); return '正常5'; }).catch(function (error) { console.log("拒绝5,传入", error); return '拒绝5'; }).then(function(data) { // This never happens: console.log("正常6,传入", data); return '正常6'; }).catch(function (error) { console.log("拒绝6,传入", error); return '拒绝6'; }); //输出 开始处理 Promise 拒绝1,传入 异常返回--> 正常2,传入 拒绝1 正常3,传入 正常2 正常4,传入 正常3 正常5,传入 正常4 正常6,传入 正常5 |
最后catch的流程:执行Promise后,忽略中途的then,直接到最后的catch方法处理。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
var jsonPromise = new Promise(function(resolve, reject) { console.log("开始处理 Promise"); //resolve("正常返回-->"); reject("异常返回-->"); }); jsonPromise.then(function(data) { // This never happens: console.log("正常1,传入", data); return '正常1'; }).then(function(data) { // This never happens: console.log("正常2,传入", data); return '正常2'; }).then(function(data) { // This never happens: console.log("正常3,传入", data); return '正常3'; }).then(function(data) { // This never happens: console.log("正常4,传入", data); return '正常4'; }).then(function(data) { // This never happens: console.log("正常5,传入", data); return '正常5'; }).then(function(data) { // This never happens: console.log("正常6,传入", data); return '正常6'; }).catch(function (error) { console.log("拒绝6,传入", error); return '拒绝6'; }); //输出: 开始处理 Promise 拒绝6,传入 异常返回--> |
所有catch的中途出错流程:报错的地方,直接转到后面第一个catch,执行完后,继续执行这个catch后面的then。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
var jsonPromise = new Promise(function(resolve, reject) { console.log("开始处理 Promise"); resolve("正常返回-->"); //reject("异常返回-->"); }); jsonPromise.then(function(data) { // This never happens: console.log("正常1,传入", data); return '正常1'; }).catch(function (error) { console.log("拒绝1,传入", error); return '拒绝1'; }).then(function(data) { // This never happens: console.log("正常2,传入", data); return '正常2'; }).catch(function (error) { console.log("拒绝2,传入", error); return '拒绝2'; }).then(function(data) { // This never happens: console.log("正常3,传入", data); NoObject.test("这里报异常");//这里没有这个对象,要报异常 return '正常3'; }).catch(function (error) { console.log("拒绝3,传入", error); return '拒绝3'; }).then(function(data) { // This never happens: console.log("正常4,传入", data); return '正常4'; }).catch(function (error) { console.log("拒绝4,传入", error); return '拒绝4'; }).then(function(data) { // This never happens: console.log("正常5,传入", data); return '正常5'; }).catch(function (error) { console.log("拒绝5,传入", error); return '拒绝5'; }).then(function(data) { // This never happens: console.log("正常6,传入", data); return '正常6'; }).catch(function (error) { console.log("拒绝6,传入", error); return '拒绝6'; }); //输出 开始处理 Promise 正常1,传入 正常返回--> 正常2,传入 正常1 正常3,传入 正常2 拒绝3,传入 ReferenceError: NoObject is not defined at testTemplate.do?templateName=PROMISE-TPL.ftl:100 at <anonymous> 正常4,传入 拒绝3 正常5,传入 正常4 正常6,传入 正常5 |
最后catch的中途出错流程:从中途报错的then直接转到最后的catch方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
var jsonPromise = new Promise(function(resolve, reject) { console.log("开始处理 Promise"); resolve("正常返回-->"); //reject("异常返回-->"); }); jsonPromise.then(function(data) { // This never happens: console.log("正常1,传入", data); return '正常1'; }).then(function(data) { // This never happens: console.log("正常2,传入", data); return '正常2'; }).then(function(data) { // This never happens: console.log("正常3,传入", data); NoObject.test("这里报异常");//这里没有这个对象,要报异常 return '正常3'; }).then(function(data) { // This never happens: console.log("正常4,传入", data); return '正常4'; }).then(function(data) { // This never happens: console.log("正常5,传入", data); return '正常5'; }).then(function(data) { // This never happens: console.log("正常6,传入", data); return '正常6'; }).catch(function (error) { console.log("拒绝6,传入", error); return '拒绝6'; }); //输出: 开始处理 Promise 正常1,传入 正常返回--> 正常2,传入 正常1 正常3,传入 正常2 拒绝6,传入 ReferenceError: NoObject is not defined at testTemplate.do?templateName=PROMISE-TPL.ftl:94 at <anonymous> |