0

I'm writing Cloud Functions very first time in NodeJS and that's working very well, but whenever I tried to deploy I got a warning like this:

warning Avoid nesting promises promise/no-nesting (line 9)

I tried to get rid of this, but there are some issues which I need to resolve:

exports.updateUserCount = functions.pubsub.schedule('every 2 minutes').onRun(context => {
    return admin.database().ref('/servers').once('value').then(snapshot =>{

        var ips = {}
        snapshot.val().forEach( (server, i) => { ips[server.label] = server.ipv4[0] })

        var promises = snapshot.val().map( (server, index) => {
            let sftp = new Client()
            return Promise.all([sftp, server.label, index, sftp.connect({ host : server.ipv4[0], port : '22', username : 'user_name', password : '*********' })])
            .then( values => {
                return Promise.all([values[0], values[0].get('/etc/openvpn/server/openvpn-status.log'), values[1], values[2]])
            }).then( data => {
                data[0].end()
                if (data[1]) {
                    let count = countWords(data[1].toString(),"client_list") - 1
                    console.log(count, `users connected now with ${data[2]} ${data[3]}`)

                    return Promise.all([ count, admin.database().ref(snapshot.child(index.toString()).ref).update({ userCount : count }) ])
                }
                console.log('file not found')
            return false
            }).catch(err => {
              console.log(err);
            });
        })

        console.log(`array ${JSON.stringify(ips)}`)
        return Promise.all(promises)
    }).catch(err => {
        console.log(err);
    });
});

If I try to put other code outside of nesting I can't get instance of sftp and it can't be declared globally because each sftp is connected to and having configs of different servers.

Any help will be appreciated and thanks is advance.

Kate Orlova
  • 3,225
  • 5
  • 11
  • 35
Hanzala
  • 1,965
  • 1
  • 16
  • 43
  • Did you do a web search for that error message? It comes up a lot. – Doug Stevenson Feb 07 '20 at 04:50
  • 1
    async/await usually helps in such cases – Jaromanda X Feb 07 '20 at 05:00
  • @DougStevenson yes I did but not found any suitable answer I'm searching from 1 week. – Hanzala Feb 07 '20 at 05:06
  • @JaromandaX can you refer any example? – Hanzala Feb 07 '20 at 05:07
  • https://stackoverflow.com/questions/54575743/nodejs-how-to-avoid-nested-then-when-using-async-await – Jaromanda X Feb 07 '20 at 05:09
  • Don't stress too much over that particular warning. It can be happily ignored. There are good reasons for nesting promises, not least of which is to [benefit from closures in order to access previous results in a .then() chain](https://stackoverflow.com/a/28250687/3478010), as is the case here. Once you've come to terms with nesting, the need to pass values through disappears and the code will become more readable (again?). – Roamer-1888 Feb 07 '20 at 06:44
  • async/await was designed mainly to avoid this problem called callback hell – Rashomon Feb 13 '20 at 20:12

0 Answers0