router.get(“test”, async ctx => { let q = await ctx.db .execute(
SELECT w.create_time as create_time,w.camera_id,w.status, w.problem_type_id,w.repair_user_id,w.no as no,wp.pic_url FROM work_flow w left OUTER JOIN work_flow_pic wp ON w.no=wp.flow_id
); let data = q.map(item => { let url = ctx.db.execute(select * from work_flow_pic where flow_id=${item.no}
); return { create_time: item.create_time, no: item.no, url: url.then(function(result) { result; }) }; }); console.log(data); let retstr =[{"ret":"0"}]
; await ctx.render(“interface_ret”, { retstr }); }); 运行后的结果是: [ { create_time: 2018-10-09T02:47:54.000Z, no: ‘153905327352986’, url: Promise { <pending> } }, { create_time: 2018-10-09T02:47:54.000Z, no: ‘153905327352986’, url: Promise { <pending> } }, { create_time: 2018-10-09T08:51:33.000Z, no: ‘153905327352987’, url: Promise { <pending> } } ]
格式太乱了- -,你这步操作是异步的加一个await 在
let url =await ctx.db.execute
//后面url直接拿
url: url
建议先回去复习下 Promise 基础,看看阮一峰的教程
@koroshi 试过了加了await 不行,直接报错,因为前面没有async。
url: url.then(function(result) {
result;
})
其实就是这个语法不对的,你要么在回调里赋值,要么await结果,不能直接把promise赋值给变量。 我也建议完整的再看一下promise
辨析
加await
提示没有async
,是因为在数组迭代中不允许用await
建议1
for(const item of q){
await ctx.db.execute...
}
建议2
用p-map,可以控制并发数
const pMap = require('p-map');
// concurrency
const mapper = article => {
// render article
return this._renderArticle({ site, article });
};
await pMap(articles, mapper, { concurrency: 10 });