Co executes the generator function, how to return data after multiple co functions have been returned?

let form = new multiparty.Form({
                encoding: "utf-8",
                // uploadDir:"public/upload",  //
                keepExtensions: true  //
            })
            form.parse(ctx.req, function (err, fields, files) {
                let data=[]
                for(let f of files.file){
                    // 
                    let date = new Date()
                    let time = "" + date.getFullYear() + (date.getMonth() + 1) + date.getDate()
                    let filepath = "project/"+time + "/" + date.getTime()
                    let fileext = f.originalFilename.split(".").pop()
                    let upfile = f.path
                    let newfile = filepath + "." + fileext
                    //ali-oss
                    co(function*() {
                        client.useBucket("p-adm-test")
                        let result = yield client.put(newfile, upfile)
                        console.log("!", result.url)
                        data.push(result.url)
                        console.log(data)
                        resolve()
                    }).catch(function(err) {
                        console.log(err)
                    })
                }
                ctx.response.type = "json"
                ctx.response.body = {
                    errno: 0,
                    data: data
                }
            })

as in the above code, I have executed the co function many times. I hope that the result returned each time will be push to the data array, and the data returned to the front end will be the data, after multiple execution, but what if the front end only receives the data, returned for the first time?


co also returns promise according to your return array, you can use promise.all

var fn = co.wrap(function*(newfile, upfile) {
  client.useBucket("p-adm-test");
  let result = yield client.put(newfile, upfile);
  return result.url;
});

1. promise.all

var arr = []
for(let f of files.file){
    arr.push(fn(newfile,upfile))
}
//Promise.all(arr,function(res){
Promise.all(arr.then(function(res){
    ctx.response.body = {
        errno: 0,
        data: res
    }
})

2. co nested


form.parse(ctx.req, co.warp(function* (err, fields, files) {
    let data = []
    for(let f of files.file){
        let res = yield fn(newfile,upfile)
        data.push(res)
    }
    ctx.response.body = {
        errno: 0,
        data: data
    }
}))
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1be1326-31568.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1be1326-31568.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?