Node.js - 将请求中的 return 值保存为变量
Node.js - Save return value from request as variable
我正在开发 node.js 应用程序并使用 Request 从 API 获取数据,使用此函数:
function do_request( method, path, body ){
var options = {
...
};
request( options, function(err, response, body){
if (err) console.log(err);
return JSON.parse(response.body);
});
}
我希望能够将返回值保存到一个变量中——像这样:
var products = do_request('GET','/products','');
目前,如果我在上面的代码之后 console.log(products)
,我会得到 undefined
。
根据我通过谷歌搜索发现的内容,它看起来可能与异步相关。我已经尝试使用基于我发现的一些 examples/solutions 的承诺和异步函数,但我是一个节点菜鸟,无法让它工作。有什么想法吗?
异步版本看起来像这样:
async function do_request( method, path, body ){
var options = {
...
};
await request( options, function(err, response, body){
if (err) console.log(err);
return JSON.parse(response.body);
});
}
或者由于 Request 已弃用,您可以/(应该?)使用类似 axios:
的内容
axios.get("request_url").then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
尝试 promisify request
库并在 async
函数中调用 do_request
(因为你可以 await
async
函数内的承诺)
function do_request( method, path, body ){
var options = {
...
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
})
}
async function main_code(){
try{
var products = await do_request('GET','/products','');
}
catch(e){
// exception handling
}
finally{
// stuff to do anyway, cleanups etc etc.
}
}
main_code();
您必须通过添加 async
关键字使您的函数成为异步函数,并且您可以使用 await
关键字调用您的函数。
然后当请求无误地完成时。您可以看到您的产品变量具有一些有意义的值。
如果您只是在函数调用行之后打印产品变量。它将是未定义的,因为届时请求尚未完成。
您可以选择的是使用 .then
(回调)而不是 await 关键字。因此,当您的请求被返回时,您的产品变量将被填充,然后您可以打印它。像这样:
do_request('GET','/products','').then((returning_value)=>{console.log(returning_value})
我正在开发 node.js 应用程序并使用 Request 从 API 获取数据,使用此函数:
function do_request( method, path, body ){
var options = {
...
};
request( options, function(err, response, body){
if (err) console.log(err);
return JSON.parse(response.body);
});
}
我希望能够将返回值保存到一个变量中——像这样:
var products = do_request('GET','/products','');
目前,如果我在上面的代码之后 console.log(products)
,我会得到 undefined
。
根据我通过谷歌搜索发现的内容,它看起来可能与异步相关。我已经尝试使用基于我发现的一些 examples/solutions 的承诺和异步函数,但我是一个节点菜鸟,无法让它工作。有什么想法吗?
异步版本看起来像这样:
async function do_request( method, path, body ){
var options = {
...
};
await request( options, function(err, response, body){
if (err) console.log(err);
return JSON.parse(response.body);
});
}
或者由于 Request 已弃用,您可以/(应该?)使用类似 axios:
的内容axios.get("request_url").then(function (response) {
// handle success
console.log(response);
})
.catch(function (error) {
// handle error
console.log(error);
})
尝试 promisify request
库并在 async
函数中调用 do_request
(因为你可以 await
async
函数内的承诺)
function do_request( method, path, body ){
var options = {
...
};
return new Promise((resolve,reject)=>{
request( options, function(err, response, body){
if (err) reject(err);
resolve(JSON.parse(response.body));
});
})
}
async function main_code(){
try{
var products = await do_request('GET','/products','');
}
catch(e){
// exception handling
}
finally{
// stuff to do anyway, cleanups etc etc.
}
}
main_code();
您必须通过添加 async
关键字使您的函数成为异步函数,并且您可以使用 await
关键字调用您的函数。
然后当请求无误地完成时。您可以看到您的产品变量具有一些有意义的值。
如果您只是在函数调用行之后打印产品变量。它将是未定义的,因为届时请求尚未完成。
您可以选择的是使用 .then
(回调)而不是 await 关键字。因此,当您的请求被返回时,您的产品变量将被填充,然后您可以打印它。像这样:
do_request('GET','/products','').then((returning_value)=>{console.log(returning_value})