【发布时间】:2015-08-09 22:30:27
【问题描述】:
我正在尝试创建一个使用承诺返回 api 调用主体的函数。我的代码是
function checkApi(link) {
var promise = new Promise(function(resolve, reject) {
//query api
});
promise.then(function(value) {
console.log(value); //want to return this, this would be the body
}, function(reason) {
console.log(reason); //this is an error code from the request
});
}
var response = checkApi('http://google.com');
console.log(response);
我想返回 google.com 的正文,而不是做控制台日志,以便我可以使用它。这只是一个范围问题,但我不知道如何解决它。谢谢,
【问题讨论】:
-
所以
returnit +return本身就是一个承诺。而且,不,你不能“解开”一个承诺结果:一旦你开始处理承诺 - 捕获其内容的唯一方法是使用.then()
标签: javascript node.js promise httprequest