Ben*_*aum 18 javascript generator promise async-await bluebird
我正在使用生成器和Bluebird编写代码,我有以下内容:
var async = Promise.coroutine;
function Client(request){
this.request = request;
}
Client.prototype.fetchCommentData = async(function* (user){
var country = yield countryService.countryFor(user.ip);
var data = yield api.getCommentDataFor(user.id);
var notBanned = yield authServer.authenticate(user.id);
if (!notBanned) throw new AuthenticationError(user.id);
return {
country: country,
comments: data,
notBanned: true
};
});
Run Code Online (Sandbox Code Playgroud)
但是,这有点慢,我觉得我的应用程序等待I/O太多而且它不是并行的.如何提高应用程序的性能?
总响应时间为800 countryFor+ 400 getCommentDataFor+ + 600,authenticate因此总共1800ms这是很多.
Ben*_*aum 18
您花费了太多时间等待来自不同来源的I/O.
在正常的承诺代码中,你可以使用Promise.all它,但是 - 人们倾向于编写等待生成器请求的代码.您的代码执行以下操作:
<-client service->
countryFor..
''--..
''--..
''--.. country server sends response
..--''
..--''
..--''
getCommentDataFor
''--..
''--..
''--..
''--.. comment service returns response
..--''
..--''
..--''
authenticate
''--..
''--..
''--.. authentication service returns
..--''
..--''
..--''
Generator done.
Run Code Online (Sandbox Code Playgroud)
相反,它应该做:
<-client service->
countryFor..
commentsFor..''--..
authenticate..''--..''--..
''--..''--..''--.. country server sends response
''--..--''.. comment service returns response
..--''..--''.. authentication service returns response
..--''..--''..
..--''..--''..--''
..--''..--''
..--''
Generator done
Run Code Online (Sandbox Code Playgroud)
简而言之,所有I/O都应该在这里并行完成.
要解决这个问题,我会用Promise.props.Promise.props获取一个对象并等待其所有属性解析(如果它们是promises).
记住 - 生成器和承诺混合搭配得很好,你只需要产生承诺:
Client.prototype.fetchCommentData = async(function* (user){
var country = countryService.countryFor(user.ip);
var data = api.getCommentDataFor(user.id);
var notBanned = authServer.authenticate(user.id).then(function(val){
if(!val) throw new AuthenticationError(user.id);
});
return Promise.props({ // wait for all promises to resolve
country : country,
comments : data,
notBanned: notBanned
});
});
Run Code Online (Sandbox Code Playgroud)
这是人们第一次使用发电机时常犯的错误.
ascii艺术无耻地从K-Kowal的Q-Connection中获取
Ber*_*rgi 11
正如Bluebird文档中提到的那样Promise.coroutine,你需要注意不要yield在一个系列中.
Run Code Online (Sandbox Code Playgroud)var county = yield countryService.countryFor(user.ip); var data = yield api.getCommentDataFor(user.id); var notBanned = yield authServer.authenticate(user.id);
此代码有3个yield表达式,每个表达式都会停止执行,直到确定特定的承诺.代码将连续创建并执行每个异步任务.
要并行等待多个任务,您应该yield有一系列承诺.这将等到所有这些都结算,然后返回结果值数组.使用ES6解构分配可以得到简洁的代码:
Client.prototype.fetchCommentData = async(function* (user){
var [county, data, notBanned] = yield [
// a single yield only: ^^^^^
countryService.countryFor(user.ip),
api.getCommentDataFor(user.id),
authServer.authenticate(user.id)
];
if (!notBanned)
throw new AuthenticationError(user.id);
return {
country: country,
comments: data,
notBanned: true
};
});
Run Code Online (Sandbox Code Playgroud)