When I do the following:
for (var CurrentRow=0;CurrentRow < qryMfg.RecordCount;CurrentRow++){
console.log(qryMfg.MFGID[CurrentRow]);
dbo.transaction(function(myTrans) {
console.log(qryMfg.MFGID[CurrentRow]);
});
}
I get a list of MfgID the way that I want, followed by a list of unknowns because dbo.transaction is executing asynchronously.
How do I pass a variable into dbo.transaction?
Variable scope is created in functions, so create a function that returns the handler…
…and invoke it in the loop, passing it whatever you need scoped, in this case, the
CurrentRow…Now each individual handler is created in the same unique scope that was created via the invocation in each iteration.
Since the
CurrentRowwas passed into that function scope, each handler will reference the unique value in its own scope via thescoped_rowparameter.When the handler is returned from the function, it will be passed to the
dbo.transaction.Even though it is passed out of the function where it was created, it will retain its original variable scope, and so will always have access to the
scoped_rowparameter.You could also put the whole operation in the function if you prefer.
…which will have the same outcome as long as you pass it the
CurrentRow…