Skip to content

Getting Back Support for Continuation Local Storage

Nicolai Kamenzky edited this page Jul 11, 2016 · 1 revision

Request-Promise v4 dropped CLS support. Now what?

With Request-Promise v4 the nearly identical request-promise-any package was released. Thanks to any-promise you can now use any Promise implementation.

If you need CLS support you may:

// 1. Install all required libraries:
// $ npm install --save continuation-local-storage bluebird cls-bluebird any-promise request-promise-any

// 2. Create your namespace
var cls = require('continuation-local-storage');
var ns = cls.createNamespace('testNS');

// 3. Add CLS support by binding the namespace to Bluebird
require('cls-bluebird')(ns);

// 4. Register Bluebird with any-promise
require('any-promise/register/bluebird');

// 5. Require Request-Promise-Any
var request = require('request-promise-any');

// 6. Do your requests
ns.run(function () {
    ns.set('value', 'hi');

    request('http://google.com')
        .then(function (body) {
            console.log(ns.get('value')); // -> hi
        });
});

Make sure step 3 and 4 are executed during startup BEFORE step 5 gets executed for the first time anywhere in the code.