使用异步 NPM 模块
Utilizing Async NPM Module
所以...我最近遇到了这个节点模块:async。对于下面的情况,我只需要一点 "show and tell" 或最佳实践方法。下面你可以看到我的函数 GetBearerToken
接受参数 {bearer_token:token}
没有问题。
我的问题是变量 ss
。我想在这个函数之外使用它,并将它传递给另一个函数来做一些事情。当然,当我尝试访问 ss
时,它是未定义的。我已经按照文档中的指示尝试了一些使这项工作有效的方法,但我显然遗漏了一些东西。所以任何帮助都会很棒...谢谢
GetBearerToken({
bearer_token: token
}, function(error, result) {
if (error) throw error;
if (result) {
var resultset
var i;
for (i = 0; i < result.length; i++) {
resultset = (simpleObjectify(result[i].meta, result[i].rows))
}
var jstring = JSON.stringify(resultset);
var obj = JSON.parse(jstring);
var ss = obj[0].uuid;
console.log(ss)
})
函数 ss
之外是 undefined
。
首先阅读这篇文章
What is the scope of variables in JavaScript?
您可以尝试使用.waterfall
方法
waterfall(tasks, [callback])
Runs the tasks array of functions in series, each passing their
results to the next in the array. However, if any of the tasks pass an
error to their own callback, the next function is not executed, and
the main callback is immediately called with the error.
举个例子说明你想要完成的事情
async.waterfall([
function(callback) {
GetBearerToken({
bearer_token: token
}, function(error, result) {
if (error) throw error;
if (result) { // *note* don't forget to handle the result properly if an error occurs or the result is not what you expected.
var resultset
var i;
for (i = 0; i < result.length; i++) {
resultset = (simpleObjectify(result[i].meta, result[i].rows))
}
var jstring = JSON.stringify(resultset);
var obj = JSON.parse(jstring);
var ss = obj[0].uuid;
callback(null, ss); // call the next callback in waterfall with ss value
}
)
},
function(arg1, callback) {
// arg1 now equals ss value
callback(null,'all completed');
}
],
function(err, result) {
// result now equals 'all completed'
});
但是由于上面的代码似乎已经朝着错误的方向迈出了一步
来调试。但是看看 .waterfall
分解回调然后调用 .waterfall
方法的例子。
所以...我最近遇到了这个节点模块:async。对于下面的情况,我只需要一点 "show and tell" 或最佳实践方法。下面你可以看到我的函数 GetBearerToken
接受参数 {bearer_token:token}
没有问题。
我的问题是变量 ss
。我想在这个函数之外使用它,并将它传递给另一个函数来做一些事情。当然,当我尝试访问 ss
时,它是未定义的。我已经按照文档中的指示尝试了一些使这项工作有效的方法,但我显然遗漏了一些东西。所以任何帮助都会很棒...谢谢
GetBearerToken({
bearer_token: token
}, function(error, result) {
if (error) throw error;
if (result) {
var resultset
var i;
for (i = 0; i < result.length; i++) {
resultset = (simpleObjectify(result[i].meta, result[i].rows))
}
var jstring = JSON.stringify(resultset);
var obj = JSON.parse(jstring);
var ss = obj[0].uuid;
console.log(ss)
})
函数 ss
之外是 undefined
。
首先阅读这篇文章
What is the scope of variables in JavaScript?
您可以尝试使用.waterfall
方法
waterfall(tasks, [callback])
Runs the tasks array of functions in series, each passing their results to the next in the array. However, if any of the tasks pass an error to their own callback, the next function is not executed, and the main callback is immediately called with the error.
举个例子说明你想要完成的事情
async.waterfall([
function(callback) {
GetBearerToken({
bearer_token: token
}, function(error, result) {
if (error) throw error;
if (result) { // *note* don't forget to handle the result properly if an error occurs or the result is not what you expected.
var resultset
var i;
for (i = 0; i < result.length; i++) {
resultset = (simpleObjectify(result[i].meta, result[i].rows))
}
var jstring = JSON.stringify(resultset);
var obj = JSON.parse(jstring);
var ss = obj[0].uuid;
callback(null, ss); // call the next callback in waterfall with ss value
}
)
},
function(arg1, callback) {
// arg1 now equals ss value
callback(null,'all completed');
}
],
function(err, result) {
// result now equals 'all completed'
});
但是由于上面的代码似乎已经朝着错误的方向迈出了一步
来调试。但是看看 .waterfall
分解回调然后调用 .waterfall
方法的例子。