admin管理员组文章数量:1305179
I'm trying to create a promise-wrapper using generator so that I can do:
var asyncResult = PromiseWrapper( $.ajax( ... ) );
So far, I've been trying with:
function PromiseWrapper(promise){
return function *wrapper(promise){
promise.then(function(result){
yield result;
}, function(err){
throw err;
});
}(promise).next().value
}
but this fails because yielding inside a normal is not allowed. Is there any work-around for this? Thank you :D
ps: I'm using babel to translate the code from es6 to es5
I'm trying to create a promise-wrapper using generator so that I can do:
var asyncResult = PromiseWrapper( $.ajax( ... ) );
So far, I've been trying with:
function PromiseWrapper(promise){
return function *wrapper(promise){
promise.then(function(result){
yield result;
}, function(err){
throw err;
});
}(promise).next().value
}
but this fails because yielding inside a normal is not allowed. Is there any work-around for this? Thank you :D
ps: I'm using babel to translate the code from es6 to es5
Share Improve this question edited Apr 28, 2015 at 7:38 Dev Doomari asked Apr 28, 2015 at 7:34 Dev DoomariDev Doomari 9971 gold badge11 silver badges19 bronze badges 2-
yielding inside promise is not allowed
- It should have beenyielding inside a normal function is not allowed
– thefourtheye Commented Apr 28, 2015 at 7:37 - @thefourtheye yep that's correct-- I'll edit it now – Dev Doomari Commented Apr 28, 2015 at 7:38
3 Answers
Reset to default 6It is utterly impossible to wrap a promise in a generator that synchronously yields the promise's result, because promises are always asynchronous. There is no workaround for that, unless you throw mightier weapons like fibers at the asynchrony.
Would this approach work for you http://davidwalsh.name/async-generators ?
A modified example from the link:
function wrap(promise) {
promise.then(function(result){
it.next( result );
}, function(err){
throw err;
});
}
function *main() {
var result1 = yield wrap( $.ajax( ... ) );
var data = JSON.parse( result1 );
}
var it = main();
it.next(); // get it all started
You should probably read the entirety of that post, the runGenerator
is a pretty neat approach.
function step1(){
return new Promise(function(c,e){
setTimeout(function(){
c(`1000 spet 1`);
},1000)
})
}
function step2(){
return new Promise(function(c,e){
setTimeout(function(){
c(`100 spet 2`);
},10000)
})
}
function step3(){
return new Promise(function(c,e){
setTimeout(function(){
c(`3000 spet 3`);
},3000)
})
}
function step4(){
return new Promise(function(c,e){
setTimeout(function(){
c(`100 spet 4`);
},100)
})
}
function *main() {
var ret = yield step1();
try {
ret = yield step2( ret );
}
catch (err) {
ret = yield step2Failed( err );
}
ret = yield Promise.all( [
step3( ret )
] );
yield step4( ret );
}
var it = main();
/*
while (true) {
var current = it.next();
if (current.done) break;
console.log(current.value);
}
*/
Promise.all( [ ...it ] ) // Convert iterator to an array or yielded promises.
.then(
function handleResolve( lines ) {
for ( var line of lines ) {
console.log( line );
}
})
本文标签: javascriptIs it possible to wrap promise inside generatorStack Overflow
版权声明:本文标题:javascript - Is it possible to wrap promise inside generator? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741791570a2397688.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论