[英]Need further help in making javascript sync generator handle async functions
I am a relative newbie in Node JS. 我是Node JS的新手。 I have a requirement to synchronously call an async function (http request()). 我需要同步调用异步函数(http request())。 This article by Kylie Simpson has helped me a lot; 凯莉辛普森的这篇文章给了我很多帮助; especially this code bit was exactly what I needed: 特别是这个代码位正是我需要的:
function request(url) {
// this is where we're hiding the asynchronicity,
// away from the main code of our generator
// `it.next(..)` is the generator's iterator-resume
// call
makeAjaxCall( url, function(response){
it.next( response );
} );
// Note: nothing returned here!
}
function *main() {
var result1 = yield request( "http://some.url.1" );
var data = JSON.parse( result1 );
var result2 = yield request( "http://some.url.2?id=" + data.id );
var resp = JSON.parse( result2 );
console.log( "The value you asked for: " + resp.value );
}
var it = main();
it.next(); // get it all started
But I need to take this one step further: I need to be able pass result1 to another function (processResult() in the example below), which then would call request() if some conditions are met. 但我需要更进一步:我需要能够将result1传递给另一个函数(下面的例子中的processResult()),如果满足某些条件,它将调用request()。 Something like this: 像这样的东西:
function request(url) {
makeAjaxCall( url, function(response){
it.next( response );
} );
}
function processResult(resSet) {
if (resSet.length>100)
return request("http://some.url.1/offset100");
else
write2File(resSet);
}
function *main() {
var result1 = yield request( "http://some.url.1" );
processResult(result1)
}
var it = main();
it.next();
But when I attempt to do this, request("http://some.url.1/offset100")
does not return any values. 但是当我尝试这样做时, request("http://some.url.1/offset100")
不会返回任何值。 Any ideas? 有任何想法吗?
But when I attempt to do this,
request("http://some.url.1/offset100")
does not return any values. 但是当我尝试这样做时,request("http://some.url.1/offset100")
不会返回任何值。
A generator function executes synchronously like any other function except when pausing it using yield
. 生成器函数与任何其他函数一样执行,除非使用yield
暂停它。
Your call to function processResult(resSet){}
is a synchronous call that returns to function *main() {}
before the async function request(url) {}
has completed. 对function processResult(resSet){}
的调用是一个同步调用,它在异步function request(url) {}
完成之前返回到function *main() {}
。 You in stead need to keep yielding out the call to the async function request(url){}
from inside the generator function. 您需要从生成器函数内部继续调用异步function request(url){}
。 You can do this by restructuring your code this way: 您可以通过以下方式重构代码来实现此目的:
function processResult(resSet) {
if (resSet.length>100)
return true;
else
write2File(resSet); // assuming that this function is synchronous
return false; // not necessary as the function in this case returns `undefined` by default which coerces to false - but it illustrates the point
}
function *main() {
var result1 = yield request( "http://some.url.1" );
if(processResult(result1)) {
var offset100 = yield request("http://some.url.1/offset100");
}
// do something with offset100
var it = main();
it.next();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.