Hãy xem hướng dẫn này về cách sử dụng Lời hứa hoặc các ví dụ bên dưới để thực hiện các lệnh gọi phương thức không đồng bộ bằng API JavaScript của Google Maps.
Không đồng bộ và chờ
Toán tử await được dùng để chờ một Promise. Bạn chỉ có thể dùng hàm này trong một hàm không đồng bộ.
constelevationService=google.maps.ElevationService();constlocations=[{lat:27.986065,lng:86.922623}];constcallback=(results,status)=>{if(status==='OK'){console.log(results);}else{// handle this case}};elevationService.getElevationForLocation({locations},callback);
Hiện tại, Promises chỉ được hỗ trợ trong getPlacePredictions(). ↩
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-08-27 UTC."],[[["\u003cp\u003eAsynchronous methods within the Google Maps JavaScript API predominantly return Promises for efficient handling of operations.\u003c/p\u003e\n"],["\u003cp\u003eNumerous Google Maps services, including Directions, Distance Matrix, Elevation, Geocoder, and Streetview, utilize Promises in their methods.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can employ async/await, then/catch/finally, or traditional callbacks to manage asynchronous operations and responses effectively.\u003c/p\u003e\n"],["\u003cp\u003eWhile Places generally do not utilize Promises, the \u003ccode\u003egetPlacePredictions()\u003c/code\u003e method within the Places AutocompleteService does offer partial Promise support.\u003c/p\u003e\n"],["\u003cp\u003eBeginning in 2020, all newly introduced APIs within the Google Maps JavaScript API exclusively support Promises for asynchronous operations.\u003c/p\u003e\n"]]],[],null,["# Promises\n\nAsynchronous methods throughout Google Maps JavaScript API return\n[Promises](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise).\n\nSupport\n-------\n\n| API | Methods return Promises |\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------------|\n| [Directions](/maps/documentation/javascript/directions) | Yes |\n| [Distance Matrix](/maps/documentation/javascript/reference/distance-matrix \"Reference documentation for Distance Matrix service\") | Yes |\n| [Elevation](/maps/documentation/javascript/reference/elevation \"Reference documentation for Elevation service\") | Yes |\n| [Geocoder](/maps/documentation/javascript/reference/geocoder \"Reference documentation for Geocoder service\") | Yes |\n| [Maximum Zoom Imagery](/maps/documentation/javascript/reference/max-zoom \"Reference documentation for MaxZoom service\") | Yes |\n| Places | No |\n| [Places AutocompleteService](/maps/documentation/javascript/reference/places-autocomplete-service#AutocompleteService \"Reference documentation for Places Autocomplete service\") | Partial^[1](#fn1)^ |\n| [Streetview](/maps/documentation/javascript/reference/street-view \"Reference documentation for StreetView service\") | Yes |\n\n| **Note:** Starting in 2020, new APIs only support Promises.\n\nUsage\n-----\n\nSee this\n[guide](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Using_Promises)\non using Promises or the examples below for making asynchronous method calls\nwith Google Maps JavaScript API.\n\n### Async and await\n\nThe\n[await operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await)\nis used to wait for a Promise. It can only be used inside an async function. \n\n const app = async () =\u003e {\n const elevationService = google.maps.ElevationService();\n const locations = [{lat: 27.986065, lng:86.922623}];\n\n const response = await elevationService.getElevationForLocation({locations});\n console.log(response.results);\n };\n\n app();\n\n### Then, catch, and finally\n\nThe\n[Promise object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#Instance_methods)\nhas `then`, `catch`, and `finally` methods that take callback functions. \n\n const elevationService = google.maps.ElevationService();\n const locations = [{lat: 27.986065, lng:86.922623}];\n\n const promise = elevationService.getElevationForLocation({locations});\n\n promise\n .then((response) =\u003e {\n console.log(response.results);\n })\n .catch((error) =\u003e {\n console.log(error);\n });\n .finally(() =\u003e {\n console.log('done');\n });\n\nAsync callback pattern\n----------------------\n\nThe\n[callback pattern](https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Introducing#Async_callbacks)\nis still valid and supported. \n\n const elevationService = google.maps.ElevationService();\n const locations = [{lat: 27.986065, lng:86.922623}];\n\n const callback = (results, status) =\u003e {\n if (status === 'OK') {\n console.log(results);\n } else {\n // handle this case\n }\n };\n\n elevationService.getElevationForLocation({locations}, callback);\n\n**Note:** Starting in 2020, new APIs only support Promises. \n\n*** ** * ** ***\n\n1. Currently Promises are only supported in `getPlacePredictions()`. [↩](#fnref1)"]]