【发布时间】:2017-11-02 03:19:26
【问题描述】:
我正在尝试使用不同的 URL 调用 getPromise 函数以返回不同的 Promise,但在第二个 Promise 的 then Success 函数中未定义。
var http=require('http');
var URL='http://localhost:3000';
var getPromise=function(url){
var promise=new Promise(function(resolve,reject){
http.get(url,function(response){
if(response.statusCode < 200 || response.statusCode > 299){
reject(new Error('ErrorCode '+response.statusCode))
}
var result="";
response.on('data',function(chunk){result +=chunk;} )
response.on('end',function(){resolve(result);} )
})
});
return promise;
}
getPromise(URL+'/olympic/2016/ranking/4')
.then(function(data){
console.log("Response "+JSON.parse(data).Country);
getPromise(URL+'/iso/country/'+JSON.parse(data).Country);
})
.then(function(data){
console.log("Data "+data)
})
.catch(function(err){
console.log(err)
});
【问题讨论】:
-
不应该返回第二个
getPromise(...)吗? -
开始使用
async/await,一切都清楚
标签: javascript node.js promise es6-promise