othiym23 / node-continuation-local-storage

implementation of https://github.com/joyent/node/issues/5243
BSD 2-Clause "Simplified" License
1.13k stars 207 forks source link

Difference with async-local-storage #125

Closed sonicoder86 closed 6 years ago

sonicoder86 commented 6 years ago

Can someone explain what is the difference between this library compared to https://github.com/vicanso/async-local-storage ? That library seems super simple.

I checked other solutions as we are having issues with destroying the namespace. Is destroying the namespace necessary on every request? Here is our middleware:

return async function(ctx, next) {
      const namespace = continuationLocalStorage.createNamespace('session');

      try{
        await new Promise(namespace.bind(function(resolve, reject) {
          namespace.set('request_id', ctx.request.header['x-request-id']);

          next().then(resolve).catch(reject);
        }));
      } catch(error) {
        throw error;
      } finally {
        continuationLocalStorage.destroyNamespace('session');
      }
    };
Qard commented 6 years ago

They are doing basically the same thing. If you are on node 8.x+ and using async functions, I'd recommend async-local-storage. This module was meant for backwards support to the AsyncListener feature in 0.x versions of node. It currently does not work with async/await though.