您的位置:首页 > 其它

Working with promise and generators

2014-08-23 07:32 375 查看
An example for working with Q and generators.

You can run it in nodejs 0.11 harmony model

var Q = require('q');

function asyncSquare(n) {
var defer = Q.defer();
setTimeout(function(){
if(n>5)
defer.reject(888)
defer.resolve(n*n);
},2000)
return defer.promise;
}

// ES6 using generator that yields promises to Q.spawn
Q.spawn(function *() {
try{
var a = yield asyncSquare(5);
console.log(a);
var b = yield asyncSquare(6);
console.log(b);
console.log('all done');
} catch(err){
console.log("err:"+err)
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: