在async.series函数中调用async.waterfall不执行async.series回调函数

时间:2017-08-23 16:57:43

标签: javascript async.js

我想在异步系列函数中调用async waterfall函数。看起来执行了并行函数,但似乎没有执行异步系列的最终回调函数。我得到函数实例的结果,该函数在系列函数中使用不同的参数调用,但由于某种原因不能执行该行。

➜  lib git:(jumpstart-compare) ✗ node aws-ecs.compare.js
Data 1f701a9754eb22ce8f0dcdb4c1b0b366a51ade9a
Data 4cc27bcc2a8482478ac2e5c0cf3ac1babe153374


var AWS = require('aws-sdk');
const async = require('async')
const _ = require('lodash');
AWS.config.update({
    region: 'us-east-1'
});
const ecs = new AWS.ECS();



getClusterSha = (clustername,app,callback) => {

    const ListServices = (callback) => {
        let params = {
            cluster: clustername
        }
        //console.log('list tasks executed')
        ecs.listServices(params, (err, data) => {
            if (err) {
                callback(err)
            } else {
                let dataObj = {
                    data: data,
                    cluster: clustername
                }
                callback(null,dataObj)
            }

        })
    }

    const getService = (arg1, callback) => {
        let appname = app
        arg1.cluster
        let finaldata = arg1.data.serviceArns.filter((elem)=>{
            if(elem.indexOf(appname) != -1){    
                return elem
            }
        });
        //console.log('finaldata: ',finaldata)
        if(finaldata.length > 0){
            callback(null,finaldata.toString().split('/')[1])
        }else{
            callback('No app with name: '+appname+' found!')
        }
    }


    const describeService = (arg2, callback) => {
        let params = {
            services: [arg2],
            cluster: clustername
        }
        ecs.describeServices(params, (err, data) => {
            if (err) {
                callback(err)
            } else {
            //  console.log(data)
                callback(null,data.services[0].taskDefinition.split('/')[1])
            }
        })
    }


    const describeTaskDef = (arg3, callback) => {
        let params = {
            taskDefinition: arg3
        }
        ecs.describeTaskDefinition(params, (err, data) => {
            if (err) {
                callback(err)
            } else {
                //console.log(data.taskDefinition.containerDefinitions[0].image.split('/')[1].split(':')[1])
                finaldata = data.taskDefinition.containerDefinitions[0]
                                                                             .image.split('/')[1]
                                                                             .split(':')[1]
                callback(null,finaldata)
            }
        })
    }


    // const githubCall = (arg4,callback) => {
    //  console.log('https://github.com/Jumpstart-Auto'+'/'+app+'/commit/'+arg4)
    //  callback(null,'https://github.com/Jumpstart-Auto'+'/'+app+'/commit/'+arg4)  
    // }


    async.waterfall([
        ListServices,
        getService,
        describeService,
        describeTaskDef,
    ], (err, data) => {
        if (err) {
                        console.log('Error', err)
                        callback(err)
        } else {
                        console.log('Data', data)
                        callback(null,data)
        }
    })

}



compareSha = (clustername1,clustername2,app) => {
    async.series([
                getClusterSha(clustername1,app,(data)=>{return data}),                  
                getClusterSha(clustername2,app,(data)=>{return data})                       
        ], (err,result)=>{
            console.log(err,result)
        })
}




compareSha('dev','staging','jamobot',function(err,data){
    console.log(data)
})

//module.exports = getShaCluster

1 个答案:

答案 0 :(得分:0)

将async.series更改为以下修复问题。

async.waterfall([
        ListServices,
        getService,
        describeService,
        describeTaskDef,
    ], (err, data) => {
        if (err) {
                        console.log('Error', err)
                        callback(err)
        } else {
                        console.log('Data', data)
                        callback(null,data)
        }
    })

}



compareSha = (clustername1,clustername2,app,cb) => {
    async.series([
            function(callback){ 
                getClusterSha(clustername1,app,callback)
          },                    
            function(callback){ 
                getClusterSha(clustername2,app,callback)
          },                    
        ], (err,result)=>{
            if(err){
                cb(err)
            }else{
                cb(null,result)
            }
        })