Search code examples
javascriptnode.jspromiseetcd

NodeJS Pending Promise


I am currently trying to write some NodeJS (v6.11.0) code to talk to etc but when I try to manipulate the data I get back the promise stays stuck in pending. The code is as follows:

const { Etcd3 } = require('etcd3');
const client = new Etcd3();

function getMembers() {
  return client.cluster.memberList()
    .then(function(value) {
      value.members.map(function(member) {
        return member.name;
      });
    });
};

console.log(getMembers());

And the output when run the CLI is:

Promise { <pending> }

I'm very new to Javascript so I'm sure I'm missing something but cannot tell what based on my reading thus far.


Solution

  • The promise is not stuck in pending. JavaScript is an asynchronous language, meaning that the console.log(getMembers()); will be executed before the promise returned by the getMembers function is resolved.

    If you want to log when the getMembers function is resolved, switch your console.log to this:

    getMembers().then(console.log);