如何在 async.js 瀑布中传递上下文或属性?
How to pass context or attributes in async.js waterfall?
我想将当前上下文或属性传递给 async.waterfall
中的函数。如何:
- 及格
this
- 一个属性(这里是
options
对象)
这是我已有的:
var _authenticate = function (cbAsync) {
var licenseId = options.licenseId; //options is undefined
};
module.exports = new Command('createOrga')
.description('creates an organization')
.option('-f, --file <file>', 'the structure file including full path')
.action(function (options) {
options.confirm = options.confirm || true; // No confirmation needed!
options.organizationUUID = (uuid.v4()).toUpperCase();
options.licenseId = (uuid.v4()).toUpperCase();
//How to pass options object to _authenticate function????
async.waterfall([ _authenticate ], function(err) {
if ( err ) {
console.warn('Error in creating new organization: ',err);
}
else {
console.info('Successfully created new organization: ' + organizationUUID);
}
});
}
}
您可以使用 Function.prototype.bind() 来 传递 变量。
async.waterfall([ _authenticate.bind(undefined, options)], function(err) {
//your code
});
因为首先传递绑定变量,所以您的回调必须如下所示:
var _authenticate = function (options, cbAsync) {
var licenseId = options.licenseId; //options is undefined
};
我想将当前上下文或属性传递给 async.waterfall
中的函数。如何:
- 及格
this
- 一个属性(这里是
options
对象)
这是我已有的:
var _authenticate = function (cbAsync) {
var licenseId = options.licenseId; //options is undefined
};
module.exports = new Command('createOrga')
.description('creates an organization')
.option('-f, --file <file>', 'the structure file including full path')
.action(function (options) {
options.confirm = options.confirm || true; // No confirmation needed!
options.organizationUUID = (uuid.v4()).toUpperCase();
options.licenseId = (uuid.v4()).toUpperCase();
//How to pass options object to _authenticate function????
async.waterfall([ _authenticate ], function(err) {
if ( err ) {
console.warn('Error in creating new organization: ',err);
}
else {
console.info('Successfully created new organization: ' + organizationUUID);
}
});
}
}
您可以使用 Function.prototype.bind() 来 传递 变量。
async.waterfall([ _authenticate.bind(undefined, options)], function(err) {
//your code
});
因为首先传递绑定变量,所以您的回调必须如下所示:
var _authenticate = function (options, cbAsync) {
var licenseId = options.licenseId; //options is undefined
};