varkoa=require('koa');varapp=koa();varunirest=require('unirest');varthunkify=require('thunkify');// Use thunkify to wrap custom async function.varget1=thunkify(function(callback){unirest.get('http://www.baidu.com').end(function(response){callback(null,response);})});// "thunkify" simple version.varget2=function(){returnfunction(done){try{unirest.get('http://www.baidu.com').end(function(response){done(null,response)})}catch(err){done(err);}}}// Exception demo.vargeterror=thunkify(function(callback){unirest.get('http://www.baidu.com').end(function(response){// throw new Error('uncaughtErrorTest') // This exception can't be caught by generator.callback(newError('uncaughtErrorTest'))})})// Simple router.app.use(function*(){varresponse;try{if(this.path==='/get1'){console.log('get1')response=yieldget1();this.body=response.body;}elseif(this.path==='/get2'){console.log('get2')response=yieldget2();this.body=response.body;}elseif(this.path==='/geterror'){console.log('geterror')this.status=500;response=yieldgeterror()this.body=response.body;}else{console.log(this.path)this.body='<a href="/get1">get1</a><br><a href="/get2">get2</a><br><a href="/geterror">geterror</a>'}}catch(e){console.log(e)this.body=e.stack;}});app.listen(3000);console.log('\r\nVisit localhost:3000 to debug it.')