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

context lose with async & await #133

Closed wuhw closed 6 years ago

wuhw commented 6 years ago
const { createNamespace, getNamespace } = require('continuation-local-storage');

const ns = createNamespace('test');

ns.run(async () => {
    ns.set('value', 1);
    console.log(ns.get('value'));
    await asyncfun();
    console.log(ns.get('value'));
});

function asyncfun() {
    return Promise.resolve();
}

//got
1
undefined
Qard commented 6 years ago

CLS does not support async/await. You can try cls-hooked. Side note though: async/await support in async_hooks is currently broken in Node.js 10.