云函数内递归的执行顺序,代码在下面
我想让await traverse(agent.data)
returnData.agent = str
这个代码全部执行完之后再往下执行,怎么办?
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
let str = []
exports.main = async (event, context) => {
switch (event.action) {
case 'statistical': { return statistical(event) }
default: {
return
}
}
}
async function statistical(event) {
var tmpData = []
var returnData = {}
var serviceList = []
var c1 = new cloud.Cloud({
resourceAppid: 'wx64dbfc5c3e44d',
resourceEnv: 'cloud1-4gbakrj73c10a',
})
await c1.init()
const db = c1.database()
var agent = await db.collection('agentList').where({
higherSalse: event.higherSalse,
higherAgent: event.higherAgent,
}).get()
str = str.concat(agent.data)
await traverse(agent.data)
returnData.agent = str
var data1 = await db.collection('serviceList').where({
higherAgent: event.higherAgent
}).get()
serviceList = serviceList.concat(data1.data)
for (var i = 0; i < str.length; i++) {
var data2 = await db.collection('serviceList').where({
higherAgent: str[i].identificationCode
}).get()
serviceList = serviceList.concat(data2.data)
}
returnData.serviceList = serviceList
return returnData
}
async function traverse(agent) {
var c1 = new cloud.Cloud({
resourceAppid: 'wx64dbfc5c3e44d',
resourceEnv: 'cloud1-4gbakrj73c10',
})
await c1.init()
const db = c1.database()
var tmpData = []
for (var i = 0; i < agent.length; i++) {
await db.collection('agentList').where({
higherAgent: agent[i].identificationCode,
}).get().then(res => {
if (res.data.length > 0) {
str = str.concat(res.data)
traverse(res.data)
}
})
}
}
你已经做到了await就是等待执行完成,然后获取结果往下走