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

Popular posts from this blog

OpenCV OpenCL: Convert Mat to Bitmap in JNI Layer for Android -

android - org.xmlpull.v1.XmlPullParserException: expected: START_TAG {http://schemas.xmlsoap.org/soap/envelope/}Envelope -

python - How to remove the Xframe Options header in django? -