Azure/azure-event-hubs-node

very strange code structure design

Closed this issue · 6 comments

const client = EventHubClient.fromConnectionString(connectionString);

client.open()
    .then(client.getPartitionIds.bind(client))
    .then(partitionIds => partitionIds.map(partitionId => client.createReceiver('$Default', partitionId, { startAfterTime: Date.now()}).then((receiver) => {
      console.log(`Created partition receiver: ${partitionId}`);
      receiver.on('errorReceived', printError);
      receiver.on('message', printMessage);
    })))
    .catch(printError);

convert to es7 async/await:

 const client = EventHubClient.fromConnectionString(connStr);

(async()=>{
  const c = await client.open(); 
  const partitionIds = await client.getPartitionIds(c);// why pass in c
  partitionIds.forEach(partitionId =>{
      const receiver = await  client.createReceiver('$Default', partitionId, { startAfterTime: Date.now()}); // why not pass in c 
      receiver.on('errorReceived', printError);
      receiver.on('message', printMessage);
  });
})();

recommend:

 const client = EventHubClient.fromConnectionString(connStr);

(async()=>{
  // open like mysql and other common packages
  client.open(); 
  const partitionIds = await client.getPartitionIds();
  partitionIds.forEach(partitionId =>{
      const receiver = await  client.createReceiver('$Default', partitionId, { startAfterTime: Date.now()});
      receiver.on('errorReceived', printError);
      receiver.on('message', printMessage);
  });
})();

So what you're saying is:

  • Client.open() should be synchronous
  • And you want the examples to use async/await syntax instead of promises?

Note that for client.getPartitionIds it doesn't "pass in c". It binds to c, meaning it sets "this" to the client in the callback

I personally can't see why it would be better to use the async/await syntax. And I don't have any opinion on whether client.open should be synchronous or asynchronous. But I'm not a committer on this project so w/e

I guess it depends on which version of node this library required, not all node versions support async/await after all.
Personally, I prefer async/await, too.

there is a thing called babel

@betarabbit 南京的工作考虑么~

Personally I far prefer the syntax they currently have over async/await. Async/await isn't really ready to be used plug-and-play and most people don't want to use a precompile step on node serverside.

So it's subjective at least. I'd rather have an example I can just cut-and-paste and be sure it's going to work

@willin @lulzmachine @betarabbit - We have a new preview version "0.1.0" of the event hubs node.js client. This client was re-written from scratch. Please try the new version and let us know if you still see issues. You can find examples over here.

You should be able to use async/await seamlessly if necessary.