Javascript 从url获取JSON的节点JS在代码的其余部分之后执行
我想从节点JS代码中的URL获取JSON。代码运行良好,但由于执行的异步性质,执行顺序混乱Javascript 从url获取JSON的节点JS在代码的其余部分之后执行,javascript,node.js,asynchronous,Javascript,Node.js,Asynchronous,我想从节点JS代码中的URL获取JSON。代码运行良好,但由于执行的异步性质,执行顺序混乱 var http = require("https"); var number = 37302; // these functions need to execute is sequence. console.log('Before API Call'); var response = fetchJson(number); console.log(response); console.l
var http = require("https");
var number = 37302;
// these functions need to execute is sequence.
console.log('Before API Call');
var response = fetchJson(number);
console.log(response);
console.log('After API Call');
function fetchJson(number)
{
var url = 'https://example.com/api/getactionitems/' + number;
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
console.log('JSON Retrieved.');
});
res.on('end', function(){
console.log('Parsing JSON');
var APIResponse = JSON.parse(body);
var Name = APIResponse.EmpName;
var outstring = APIResponse.ActionItem;
return ('Hi ' + Name + ', Your action Items are: '+ outstring);
});
})
.on('error', function(e){
return ("Got an error while fetching data.");
});
}
执行此代码时,输出字符串的顺序如下所示:
Before API Call
undefined
After API Call
JSON Retrieved.
Parsing JSON
如何更正执行顺序,使顺序如下所示:
Before API Call
JSON Retrieved.
Parsing JSON
<Outpt from the JSON parsing>
After API Call
API调用之前的
检索到JSON。
解析JSON
API调用后
Hi Shirish,谢谢,但由于某种原因,此解决方案不会打印“API调用后”字符串。您在结尾处得到了什么console.log“获取数据时出错”。或者“Hi”+Name+”,您的操作项是:“+Outstring我收到此错误:TypeError:res不是IncomingMessage的函数。(d:\Development\NodeJSProjects\assistant.js:42:13)在IncomingMessage.emit(events.js:215:7)在endReadableNT(_stream_readable.js:1184:12)在processtick和rejections(internal/process/task_queues.js:80:21)很抱歉再次打扰您:-(但是现在响应是这样的。在API调用之前检索到JSON.Parsing JSON Promise{})API Calli再次编辑答案后,我确信这会起作用Hi Shirish,谢谢,但由于某种原因,此解决方案不会打印“API调用后”字符串。最后您得到了什么console.log“获取数据时出错”或者“Hi”+Name+”,您的操作项是:“+Outstring我收到此错误:TypeError:res不是IncomingMessage的函数。(d:\Development\NodeJSProjects\assistant.js:42:13)在IncomingMessage.emit(events.js:215:7)在endReadableNT(_stream\u readable.js:1184:12)在ProcessTicks和Rejections(internal/process/task_queues.js:80:21)很抱歉再次打扰您:-(但是现在的响应是这样的。在API调用JSON检索之前。解析JSON承诺{}在API Calli再次编辑答案之后,我确信这会起作用
var http = require("https");
var number = 37302;
// these functions need to execute is sequence.
console.log('Before API Call');
fetchJson(number).then(function(res){
console.log(res);
console.log('After API Call');
}).catch(function(e){console.log('err',e)});
function fetchJson(number)
{
return new Promise(function(resolve,reject){
var url = 'https://example.com/api/getactionitems/' + number;
http.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
console.log('JSON Retrieved.');
});
res.on('end', function(){
console.log('Parsing JSON');
var APIResponse = JSON.parse(body);
var Name = APIResponse.EmpName;
var outstring = APIResponse.ActionItem;
resolve('Hi ' + Name + ', Your action Items are: '+ outstring);
});
})
.on('error', function(e){
reject("Got an error while fetching data.");
});
});
}