摘要:原生提供了对象,大大简化了的代码维护难度。实际实现远离并不复杂,仅需要十几行代码,就能实现类似的效果实际还是有点区别的。简单的实现的函数原文地址执行如果执行函数会同步返回结果,则调用。
ES6 原生提供了 Promise 对象,大大简化了 callback 的代码维护难度。使用promise对象之后可以使用一种链式调用的方式来组织代码;让代码更加的直观。
如果想在老浏览器中使用 Promise,需要使用第三方库。实际实现远离并不复杂,仅需要十几行代码,就能实现类似的效果(实际还是有点区别的)。
// 简单的实现 Promise 的函数 // 原文地址:http://www.miaoqiyuan.cn/p/promise var PromiseDemo = function(fun, _status_code){ this._status_code = _status_code || "status"; //执行 this.run = function(){ this._result = fun.call(this, fun); //如果执行函数会 同步 返回结果,则调用callback。 if( !! this._result ){ return this.callback(); }; }; //回调函数,如果不是立即返回结果,需要手动调用 this.callback = function(_result){ //异部调用时,传入执行结果 if( !!_result ){ this._result = _result; } //如过状态不是 object this._result = this._result || {}; //如果没有指定 【返回状态】 值,如果没有,则使用 status this._status = this._result[this._status_code] || "fail"; /* 如果 【返回状态】 已经定义了 回调函数,调用本状态回调函数 如果 【返回状态】 没有定义,则调用 _default 函数 如果 【返回状态】 没有定义,并且没有调用 _default 函数,抛出异常 */ this._callback = this[this._status] || this._default || function(){ throw new Error("Undefined " + this._status); }; return this._callback.call(this); }; //then判断 this.then = function(_status, callback){ if( typeof _status == "function" ){ //没有指定状态 callback = _status; if( !("success" in this) ){ //没有 success,则将 callback 设置为 success 状态的回调函数 _status = "success"; }else if( !("fail" in this) ){ //没有 fail,则将 callback 设置为 fail 状态的回调函数 _status = "fail"; }else{ // 如果 success 和 fail 已经设置,无论调用多少次,都是 默认状态 的回调函数 _status = "_default"; }; }; //设置 【返回状态】 的回调函数 this[_status] = callback; //链式操作 return this; }; //链式操作 return this; }
就这么几行代码,就实现了简单的 Promise。为了方便测试,写成函数
var PromiseTest = function(fun, _status_code, _default){ if( typeof _status_code == "function" ){ _default = _status_code; _status_code = undefined; } var pTest = new PromiseDemo(fun, _status_code); pTest.then(function(){ console.log("Success!"); console.log(this._result); }); pTest.then(function(){ console.log("Fail!"); console.log(this._result); }); if( typeof _default == "function"){ pTest.then(_default); }; return pTest; }
下面的代码用于测试效果: 返回成功状态
PromiseTest(function(){ return { status: "success"}; }).run(); /* Success! Object {status: "success"} */
返回失败状态
PromiseTest(function(){ return { status: "fail"}; }).run(); /* Fail! Object {status: "fail"} */
返回其他状态,没有定义,抛出异常
PromiseTest(function(){ return { status: "other"}; }).run(); /* Uncaught Error: Undefined other(…) */
修改 【返回状态】 参数,返回成功状态
PromiseTest(function(){ return { status: "other", code : "success"}; }, "code").run(); /* Success! Object {status: "other", code: "success"} */
增加默认值函数,所有未定义的状态,都是用此回调函数
PromiseTest(function(){ return { status: "other"}; }, function(){ console.log("Other"); }).run(); /* Other */
自定义状态值,返回 nicai 状态
PromiseTest(function(){ return { status: "nicai"}; }).then("wocai", function(){ console.log("Wocai"); }).then("nicai", function(){ console.log("Nicai"); }).run(); /* Nicai */
同步调用有返回值
PromiseTest(function(){ return { status: "nicai", value : "abc"}; }).then("nicai", function(){ console.log("Nicai"); return this._result.value; }).run() == "abc"; /* Nicai true */
异部调用测试:setTimeout
PromiseTest(function(){ setTimeout( (function(){ this.callback({ status : "success" }); }).bind(this), //必须bind,否则函数内部的 this == window 1000); }).run(); /* Success! Object {status: "success"} */
异部调用测试:Ajax
PromiseTest(function(){ $.ajax({ type : "POST", url : "/services/PinYin", data : {input:"测试"}, success : (function(result){ this.callback({ status : "success", result : result }); }).bind(this), //通过 bind 改变 this 的指向 error : (function(){ this.callback(); }).bind(this) }); }).run(); //成功 /* Success! Object {status: "success", result: "Ceshi"} */ //失败 /* Fail! Object {} */
异部调用测试:如果需要用 this 访问 jQuery 的 ajax 对象
PromiseTest(function(){ var me = this; //在本函数内,用可以 me 指向 this(PromiseDemo的实例); $.ajax({ type : "POST", url : "/services/PinYin", data : {input:"测试"}, success : (function(result){ me.callback({ status : "success", result : result }); }), error : (function(){ me.callback(); }) }); }).run();
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/91013.html
摘要:近几年随着开发模式的逐渐成熟,规范顺势而生,其中就包括提出了规范,完全改变了异步编程的写法,让异步编程变得十分的易于理解。最后,是如此的优雅但也只是解决了回调的深层嵌套的问题,真正简化异步编程的还是,在端,建议考虑。 本篇,简单实现一个promise,主要普及promise的用法。 一直以来,JavaScript处理异步都是以callback的方式,在前端开发领域callback机制...
摘要:实现的一个简单的如果有错误的地方,希望大家能够不吝赐教仅实现及方法最下方有完整代码开始一个对象接收的是一个这个接收两个参数当我们在内执行或的时候,就会调用内定义的和函数然后,和函数会改变的状态所以它应该是像下面这样的保存值记录状态为,为,为 实现的一个简单的ES6 Promise(如果有错误的地方,希望大家能够不吝赐教) 仅实现Promise及.then方法最下方有完整代码 开始 一个...
摘要:在和方法执行的时候订阅事件,将自己的回调函数绑定到事件上,属性是发布者,一旦它的值发生改变就发布事件,执行回调函数。实现和方法的回调函数都是,当满足条件对象状态改变时,这些回调会被放入队列。所以我需要在某个变为时,删除它们绑定的回调函数。 前言 按照文档说明简单地实现 ES6 Promise的各个方法并不难,但是Promise的一些特殊需求实现起来并不简单,我首先提出一些不好实现或者容...
摘要:简单实现前言你可能知道,的任务执行的模式有两种同步和异步。你已经实现了方法方法是一个很好用的方法。感兴趣的朋友可以自行去研究哈附上代码完整的实现个人博客链接 Promise 简单实现 前言 你可能知道,javascript 的任务执行的模式有两种:同步和异步。 异步模式非常重要,在浏览器端,耗时很长的操作(例如 ajax 请求)都应该异步执行,避免浏览器失去响应。 在异步模式编程中,我...
摘要:为了降低异步编程的复杂性,所以。难理解请参考的误区以及实践异步编程的模式异步编程的种方法 异步编程 javascript异步编程, web2.0时代比较热门的编程方式,我们平时码的时候也或多或少用到,最典型的就是异步ajax,发送异步请求,绑定回调函数,请求响应之后调用指定的回调函数,没有阻塞其他代码的执行。还有像setTimeout方法同样也是异步执行回调的方法。 如果对异步编程...
摘要:近几年随着开发模式的逐渐成熟,规范顺势而生,其中就包括提出了规范,完全改变了异步编程的写法,让异步编程变得十分的易于理解。最后,是如此的优雅但也只是解决了回调的深层嵌套的问题,真正简化异步编程的还是,在端,建议考虑。 前段时间频频看到Promise这个词,今天发现腾讯AlloyTeam写得这篇很赞,遂转之。 原文链接 本篇,主要普及promise的用法。 一直以来,JavaScrip...
阅读 1285·2023-04-25 19:10
阅读 1159·2021-09-10 10:50
阅读 3042·2021-09-02 15:21
阅读 1401·2019-08-30 15:52
阅读 1696·2019-08-30 13:56
阅读 2099·2019-08-30 12:53
阅读 1886·2019-08-28 18:22
阅读 2136·2019-08-26 13:47