node.js - How could I handle timeout request in koa? -
for example:
var app = require('koa')(); app.use(function *(next) { var start = new date(); yield next; }); app.use(function *(next) { var result = yield loaddata(); this.body = result; }); app.listen(8080); let's assume if loaddata returns data more 1 second, want this.body = 'there timeout'. how achieve this? don't think settimeout able deal this. , tried this.response.settimeout function, said settimeout undefined. suggestion
you can convert promise instead.
`loaddata().then((oncompletion) => { //take time here timeout = timeend - timestart; timeout >= 1sec ? this.body = 'there timeout' : this.body = oncompletion }).((onfailure) => { //do if fails });` of course not work unless loaddata returns promise
Comments
Post a Comment