码农干货系列【17】--Wind.js与Promise.js
2013-05-01 12:01 【当耐特】 阅读(2292) 评论(1) 编辑 收藏 举报示例
先引入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())之后掉链子的问题。