码农干货系列【17】--Wind.js与Promise.js - 【当耐特】
by 【当耐特】
at 2013-05-01 12:01:00
original http://www.cnblogs.com/iamzhanglei/archive/2013/05/01/3052980.html
示例
先引入wind.js与promise.js:
<script src="wind-all-0.7.3.js"></script> <script src="promise.js"></script>
然后create:
Wind.Promise.create = function (fn) { var prms = Promise(); fn(prms.resolve, prms.reject); return prms; }
最后:
var testAsync = eval(Wind.compile("promise", function () { for (var i = 0; i < 3; i++) { //loop 3 times var aa = $await(f1()); alert(aa); //alert "from f1" $await(f2().wait(3000)); //sleep 3000ms $await(f3()); } })); testAsync(); function f1() { var promise = Promise(); setTimeout(function () { console.log(1); promise.resolve("from f1"); }, 2500) return promise; } function f2() { var promise = Promise(); setTimeout(function () { console.log(2); promise.resolve(); }, 1500) return promise; } function f3() { var promise = Promise(); setTimeout(function () { console.log(3); promise.resolve(); }, 1500) return promise; }
Todo
解决$await(testAsync())之后掉链子的问题。
扩展阅读
本文链接:http://www.cnblogs.com/iamzhanglei/archive/2013/05/01/3052980.html,转载请注明。