本文共 959 字,大约阅读时间需要 3 分钟。
啥也不说贴代码,项目用
/** * 迷你版的deferred */ function Deferred(func) { if (this instanceof Deferred === false) { return new Deferred(func) } var tuple = []; var promise = { resolve: function() { var t = tuple.shift(), n; t && (n = t.apply(null, arguments), n instanceof Deferred && (n.tuple = tuple)); }, then: function(n) { return tuple.push(n), this; } } if (func) { func.call(promise, promise.resolve); } return promise; };
demo1
var d = new Deferred(); setTimeout(function() { d.resolve('aaaa') }, 500) d.then(function(bbb){ console.log(bbb) })
demo2
1 2 3 4 5 | Deferred( function (resolve) { resolve( 'aaaa' ) }).then( function (bbb) { console.log(bbb) }) |
本文转自艾伦 Aaron博客园博客,原文链接:http://www.cnblogs.com/aaronjs/p/3745607.html,如需转载请自行联系原作者