Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

context is lost in async waterfall #120

Open
LiorRabin opened this issue Jun 26, 2017 · 0 comments
Open

context is lost in async waterfall #120

LiorRabin opened this issue Jun 26, 2017 · 0 comments

Comments

@LiorRabin
Copy link

LiorRabin commented Jun 26, 2017

I'm trying to use continuation-local-storage module to have "currentUser" across my entire application.

When calling async waterfall the context gets lost.

		app.get('/testing/cls',
			function(req, res, next) {
				ns.run(function() {
					ns.set('currentUser', req.user._id)
			    	return next()
				})
			},
			function(req, res) {
				function fn1(cb) {
					console.log('async fn1', ns.get('currentUser'))
					return cb()
				}

				function fn2(cb) {
					console.log('async fn2', ns.get('currentUser'))
					cb()
				}

				function fn3(cb) {
					console.log('async fn3', ns.get('currentUser'))
					cb()
				}

				async.waterfall([
					fn1,
					fn2,
					fn3
				], function() {
					console.log('async waterfall done', ns.get('currentUser'))
					res.send({user: ns.get('currentUser')})
				})
			}
		)

The console prints are

27/6/2017-11:49:39 - info: (13405) - async fn1 58a1adaslkdjh32e
27/6/2017-11:49:39 - info: (13405) - async fn2
27/6/2017-11:49:39 - info: (13405) - async fn3
27/6/2017-11:49:39 - info: (13405) - async waterfall done

Fixing this problem is by wrapping ns.bind(fn2) but this means that need to change entire application code for wherever I have async.

What am I doing wrong?

Any help is appreciated :)

@LiorRabin LiorRabin changed the title context is lost in after callback context is lost after callback & in async waterfall Jun 26, 2017
@LiorRabin LiorRabin changed the title context is lost after callback & in async waterfall context is lost after callback from mongoose Jun 26, 2017
@LiorRabin LiorRabin changed the title context is lost after callback from mongoose context is lost after callback from mongoose and on async waterfall Jun 27, 2017
@LiorRabin LiorRabin changed the title context is lost after callback from mongoose and on async waterfall context is lost in async waterfall Jun 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant