From 935e031d3fc6bfa5eb029945cb1208eff46804f5 Mon Sep 17 00:00:00 2001 From: louis Date: Thu, 28 Sep 2023 21:59:38 +0800 Subject: [PATCH] feat: add abort controller usage --- Geocoder.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/Geocoder.js b/Geocoder.js index b9f5dfb..f80fb98 100644 --- a/Geocoder.js +++ b/Geocoder.js @@ -5,6 +5,7 @@ let Geocoder; export default Geocoder = { apiKey : null, options : {}, + instance: null, /** * Initialize the module. @@ -86,13 +87,19 @@ export default Geocoder = { // fetch try { - response = await fetch(url); + const abortController = new AbortController(); + this.instance = fetch(url, { + signal: abortController.signal + }) + response = await this.instance; } catch(error) { throw { code : Geocoder.Errors.FETCHING, message : "Error while fetching. Check your network.", origin : error, }; + } finally { + this.instance = null } // parse @@ -117,6 +124,16 @@ export default Geocoder = { return data; }, + isProcessing() { + return !!this.instance + }, + + abort() { + if (this.instance) { + this.instance.abort() + } + }, + /** * All possible errors. */