简体   繁体   中英

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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM