【发布时间】:2014-10-21 05:19:09
【问题描述】:
我想使用这样的 Promise 调用 Google Maps Geocoding API:
function makeGeoCodingRequest(address,bounds)
{
/*
Input parameters:
address:a string
bounds: an object of class google.maps.LatLngBounds(southWest,northEast)
This will return a set of locations from the google geocoding library for the given query
*/
var url="https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=AIzaSyD9GBloPC20X-1kWRo7sm_0z5xvCiaSd3c";
var promise,response;
var messages={
"ZERO_RESULTS":"No results were found",
"OVER_QUERY_LIMIT":"We are over the query limit.Wait awhile before making a request",
"REQUEST_DENIED":"Request was denied,probably using a bad or expired API Key",
"INVALID_REQUEST":"Request was sent without the required address,component or component",
"UNKNOWN_ERROR": "There was an error somewhere on Google's servers"
};
if(address)
promise=Q($.ajax({
type: "GET",
url: "https://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&key=API_KEY"
}));
return promise.then(function(data) {
if (data.status === "OK") return data;
else console.error(messages[data.status]);
return null;
});
}
当我调用函数makeGeoCodingRequest请求时,我发现我得到的是一个promise而不是一个值:
var geo=makeGeoCodingRequest(address);
console.log(Q.isPromise(geo));//returns true
为什么在返回值之前没有执行 promise.then?如何从这个 Promise 而不是另一个 Promise 中获取值?
【问题讨论】:
-
promise 不给出值,而是使用
.then()将要使用该值的函数传入 Promise
标签: javascript jquery ajax promise q