Asynchronous request processing method and device and electronic equipment
Simplifying asynchronous request processing through response interceptors and preset global parameters solves the problems of asynchronous request processing complexity and code redundancy, implements a concise and clear error handling process, and improves the stability and maintainability of the system.
Patent Information
- Application Number
- CN202410957566.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-10-29
AI Technical Summary
Existing asynchronous request processing methods are complex and have redundant codes, which increase developers’ programming complexity and system maintenance difficulty, especially in terms of error handling and status management.
Process the response data of asynchronous requests through response interceptors, use preset global parameters and interceptor logic to simplify the construction and error handling process of asynchronous requests, and reduce the need for developers to write code for different requests.
It simplifies the processing process of asynchronous requests, improves the readability and maintainability of the code, reduces the difficulty of system maintenance, and ensures the stable operation of the system and the simplicity of error handling.
Smart Images

Figure CN118869818A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of special effect technology, and in particular to a method, device and electronic device for processing asynchronous requests. Background Art
[0002] With the rapid development of Web applications, users have higher requirements for the real-time and response speed of page interaction. As a key technology to achieve this requirement, asynchronous requests allow Web applications to exchange data with the server without reloading the entire page, thus providing a smooth user experience.
[0003] In the related technology, the native XMLHttpRequest API requires developers to manually handle errors, including setting up a Promise-based interface, which simplifies the processing of asynchronous requests. However, it stores and responds to various network error states in network error handling, which increases the complexity of programming. Although the Fetch API provides inadequacies, it will not automatically reject failed requests, and developers need to manually check the response status. The error handling process for asynchronous requests in the above method is relatively complicated, and the code is redundant, which is not conducive to the stable operation of the system. Summary of the invention
[0004] In view of this, the purpose of the present disclosure is to provide a method, device and electronic device for processing asynchronous requests to simplify the code used to generate asynchronous requests and simplify the error handling process of response data, which is beneficial to system maintenance.
[0005] In a first aspect, an embodiment of the present disclosure provides a method for processing an asynchronous request, the method comprising: in response to an asynchronous request trigger message, determining an asynchronous request parameter in the asynchronous request trigger message; the asynchronous request parameter is used to indicate at least one of the following: a server address, a data request method, and related business data; based on the asynchronous request parameter and a preset asynchronous request global parameter, constructing a target asynchronous request, sending the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving response data corresponding to the target asynchronous request, calling a pre-registered response interceptor to process the response data to obtain a processing result; if the processing result indicates that the response to the target asynchronous request fails, processing the response data through a preset first data processing logic.
[0006] In a second aspect, an embodiment of the present disclosure provides an asynchronous request processing device, which includes: an asynchronous request parameter acquisition module, which is used to obtain asynchronous request parameters in response to a generation trigger event of an asynchronous request; the asynchronous request parameters are used to indicate at least one of the following: a server address, a data request method and related business data; a target asynchronous request construction module, which is used to construct a target asynchronous request based on the asynchronous request parameters and preset asynchronous request global parameters, and send the target asynchronous request to the server so that the server returns response data based on the target asynchronous request; an interceptor processing module, which is used to call a pre-registered response interceptor to process the response data in response to receiving response data corresponding to the target asynchronous request to obtain a processing result; a response data processing module, which is used to process the response data through a preset first data processing logic if the processing result indicates that the response to the target asynchronous request fails.
[0007] In a third aspect, an embodiment of the present invention provides an electronic device, including a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the above-mentioned asynchronous request processing method.
[0008] In a fourth aspect, an embodiment of the present invention provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the above-mentioned asynchronous request processing method.
[0009] The embodiments of the present invention bring the following beneficial effects:
[0010] The above-mentioned asynchronous request processing method, device and electronic device, in response to an asynchronous request trigger message, determine the asynchronous request parameters in the asynchronous request trigger message; the asynchronous request parameters are used to indicate at least one of the following: server address, data request method and related business data; based on the asynchronous request parameters and the preset asynchronous request global parameters, construct a target asynchronous request, send the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving the response data corresponding to the target asynchronous request, call the pre-registered response interceptor to process the response data to obtain the processing result; if the processing result indicates that the response to the target asynchronous request fails, the response data is processed by the preset first data processing logic. This method does not require developers to write code for different asynchronous requests. By pre-setting global parameters, the target asynchronous request can be constructed in combination with the current asynchronous request method, making the code concise and easy to maintain; developers can also write the processing logic of the interceptor, determine the situation of asynchronous request response failure through the interceptor, and then process the error data. The processing flow is relatively concise and conducive to the stable operation of the system.
[0011] Other features and advantages of the present disclosure will be described in the following description, and partly become apparent from the description, or understood by practicing the present disclosure. The purpose and other advantages of the present disclosure are realized and obtained by the structures particularly pointed out in the description, claims and drawings.
[0012] In order to make the above-mentioned objectives, features and advantages of the present disclosure more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] In order to more clearly illustrate the specific embodiments of the present disclosure or the technical solutions in the prior art, the drawings required for use in the specific embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without paying any creative work.
[0014] Figure 1 A flowchart of a method for processing an asynchronous request provided by an embodiment of the present disclosure;
[0015] Figure 2 A schematic diagram of the structure of an asynchronous request processing device provided by an embodiment of the present disclosure;
[0016] Figure 3 A schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0017] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present disclosure, rather than all of the embodiments. Based on the embodiments in the present disclosure, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present disclosure.
[0018] With the rapid development of Web applications, users have higher requirements for the real-time and responsiveness of page interactions. Asynchronous requests, as a key technology to achieve this requirement, allow Web applications to exchange data with the server without reloading the entire page, thereby providing a smooth user experience. This technology is usually implemented through AJAX (Asynchronous JavaScript and XML), which enables front-end applications to send HTTP (Hypertext Transfer Protocol) requests and process responses without blocking the user interface.
[0019] In the front-end architecture, asynchronous requests are widely used in data acquisition, form submission, real-time communication and other aspects. It enables the front-end application to dynamically obtain data from the server and display the latest information on the user interface. In addition, asynchronous requests also support real-time interaction between the front-end application and the back-end service, providing a technical foundation for building complex single-page applications (SPA).
[0020] Although asynchronous requests bring many benefits, they also bring a series of challenges in actual development. First, the management and maintenance of asynchronous requests require extra work, especially in large projects, a large number of asynchronous requests may make the code difficult to manage and maintain. Second, error handling and state management become complicated in asynchronous requests, and developers need to handle network errors, data validation errors, etc. in multiple places. In addition, the testing and debugging of asynchronous requests are also more complicated than synchronous code.
[0021] Asynchronous requests are closely related to the front-end architecture. They are not only a bridge for communication between the front-end and the back-end, but also an important part of the front-end architecture design. A good front-end architecture should be able to effectively organize and manage asynchronous requests to ensure the maintainability, scalability and testability of the code. As the complexity of front-end applications continues to increase, how to efficiently integrate and manage asynchronous requests has become a key issue in front-end architecture design.
[0022] In the field of front-end development, the implementation of asynchronous requests usually relies on the XMLHttpRequest object in native JavaScript or the modern Fetch API. In addition, many popular third-party libraries and frameworks, such as jQuery's $.ajax method, also provide simplified asynchronous request interfaces. These methods are effective in implementing basic asynchronous communication, but they may encounter some limitations and challenges when dealing with more complex application scenarios.
[0023] The native XMLHttpRequest API requires a lot of boilerplate code when writing asynchronous requests, which not only increases the development workload, but also reduces the readability and maintainability of the code. At the same time, it requires developers to manually handle errors, including setting and responding to various network error states, which increases the complexity of programming. In addition, since XMLHttpRequest does not have built-in support for Promise, it makes it difficult to control and chain asynchronous processes, which is not conducive to the asynchronous programming model of modern JavaScript.
[0024] Although the Fetch API (Application Programming Interface) provides a Promise-based interface that simplifies the processing of asynchronous requests, it has deficiencies in network error handling and does not automatically reject failed requests, requiring developers to manually check the response status. In addition, the Fetch API lacks support for global request settings, such as unified request headers or base URLs, which limits its flexibility in large projects. At the same time, it has insufficient support for older versions of browsers and may require additional polyfills to ensure compatibility.
[0025] Relying on third-party libraries such as jQuery's $.ajax for asynchronous request processing simplifies request writing, but increases the complexity of the project's external dependencies, which may lead to application size expansion and compatibility issues. In addition, dependence on third-party libraries also means that developers need to pay attention to library updates and maintenance, which may be affected by the library author's update strategy, bringing uncertainty to the long-term maintenance of the project.
[0026] In existing asynchronous request processing solutions, code duplication and dispersion is a common problem, especially in large projects, which makes it difficult to uniformly manage and maintain the code. The lack of a centralized management mechanism for asynchronous requests makes global configuration and error handling complicated, increasing the difficulty of development and maintenance. At the same time, the testing of asynchronous requests requires simulating network responses, which increases the complexity and workload of testing and places higher demands on the development team.
[0027] Based on this, the embodiments of the present disclosure provide a method, device and electronic device for processing asynchronous requests, and the technology can be applied to scenarios where asynchronous requests need to be made.
[0028] See also Figure 1 First, a method for processing an asynchronous request provided by an embodiment of the present invention is introduced. The method comprises the following steps:
[0029] Step S102, in response to the asynchronous request trigger message, determining the asynchronous request parameters in the asynchronous request trigger message; the asynchronous request parameters are used to indicate at least one of the following: server address, data request method and related business data.
[0030] When a user obtains or submits data through a web page or application, or when a client needs to obtain data from a server in other situations, an asynchronous request trigger message is usually generated. Therefore, the asynchronous request trigger message can be generated based on the user's operation or automatically generated based on a preset process, which is not limited here.
[0031] The above-mentioned asynchronous request trigger message usually includes asynchronous request parameters. The asynchronous request parameters need to include part of the address or all of the address of the server that receives the asynchronous request. The server address refers to the unique identifier of the server on the Internet, which can be composed of numbers or characters. On the Internet, each device needs an address to communicate with each other. The server address is the "house number" of the server, which is used to identify the unique location of the server on the Internet. The server address can be an IP address, domain name, Uniform Resource Locator (Uniform Resource Locator, referred to as "URL"), etc. Taking URL as an example, the URL consists of 4 parts: protocol, host, port, and path.
[0032] Since asynchronous requests are usually HTTP requests, the protocol of asynchronous requests is usually HTTP. This part is a common URL part and does not need to be transmitted through asynchronous request messages. Therefore, the asynchronous request message does not need to include the protocol part of the URL. When asynchronous requests are all sent to the same host and received by the same port of the host, the host and port parts of the URL can be used as pre-set parameters and do not need to be transmitted through asynchronous request messages. Similarly, the asynchronous request message does not need to include the host and port parts of the URL.
[0033] The asynchronous request message usually also includes parameters for indicating the data request method. Common data request methods are GET and POST. GET is used to request data from a specified resource; POST is used to submit data to be processed to a specified resource. The parameter used to indicate the data request can be a string including get or pose, or other preset strings, which are not limited here. A default data request method, such as get, can also be preset. When the data request method corresponding to the asynchronous request message is get, the parameter indicating the data request method can be omitted in the message.
[0034] The asynchronous request message usually also includes the relevant business data of the asynchronous request. The above-mentioned relevant business data is usually used by the server to find the corresponding response data, similar to searching for keywords; the relevant business data can also be the business data submitted to the server, which usually corresponds to the data request method of POST.
[0035] Step S104, constructing a target asynchronous request based on the asynchronous request parameters and preset asynchronous request global parameters, and sending the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request.
[0036] The above-mentioned asynchronous request global parameters (referred to as "global parameters") can be common parameters on the one hand. As mentioned above, when the asynchronous request usually adopts the http protocol, the protocol part of the URL is the global parameter. On the other hand, the global parameter can also be a default parameter. For example, when the default data request method is set to get, the get method parameter can also be used as an asynchronous request global parameter. For the default parameter, if the asynchronous request parameter has a parameter corresponding to the default parameter, the default parameter needs to be replaced with the corresponding parameter. For example, the default parameter is the get method parameter, and the asynchronous request parameter indicates that the data request method is post, then the get method parameter needs to be replaced with the post method parameter. Further, all the parameters required for the asynchronous request can be determined.
[0037] After obtaining all the parameters required for the asynchronous request, the target asynchronous request can be constructed. An asynchronous request usually has a preset structure, such as a request header and a request body. In the specific implementation, the request header and request body of the asynchronous request can be generated based on the parameters required for the asynchronous request, and combined to form an asynchronous request.
[0038] After sending the target asynchronous request to the server, the server will parse the target asynchronous request and generate corresponding response data based on the parsing result. The response data can be the server's own resources or the calculation result obtained by the server based on the business data in the target asynchronous request. It can be set according to the needs and is not limited here. In some cases, the response may fail to be sent. For example, due to network reasons, the server times out and returns the response data, or the server does not find the requested business data.
[0039] Step S106, in response to receiving the response data corresponding to the target asynchronous request, calling the pre-registered response interceptor to process the response data to obtain a processing result.
[0040] Interceptor is a very important component in Spring MVC. It is mainly used for specified data. It can execute pre-set code before or after the specified method according to business needs. The above response interceptor is a kind of interceptor. Since it is used to intercept response data, it is called response interceptor.
[0041] Response interceptors can be customized by developers to process response data. There can be multiple response interceptors, and the data processing logic of each response interceptor is usually different. The processing order of multiple response interceptors is related to their registration order. Developers can write the logic code of the response interceptor according to their needs so that the response interceptor executes the corresponding business logic.
[0042] The response interceptor can determine whether the response data indicates a response failure based on the preset business logic. For example, multiple parameters can be pre-set, some of which are parameters that should be included in the response data in various response success situations, and the other part are parameters that should be included in the response data in various response failure situations, and then the received response data is compared with these parameters. If the response data includes parameters in a certain response failure situation, it is confirmed that the response data indicates a response failure. The above is only an example, which can be set based on needs and is not limited here.
[0043] Step S108: If the processing result indicates that the response to the target asynchronous request fails, the response data is processed by a preset first data processing logic.
[0044] If the processing result indicates that the response to the target asynchronous request fails, it needs to be processed according to the preset data processing logic for processing errors, that is, the first data processing logic mentioned above.
[0045] When the response data is processed by the first data processing logic, the response parameter indicating the response failure in the response data can be sent to the specified server, or stored in the specified location, and the relevant log information can also be stored for subsequent analysis and processing. It is also possible to continue to try to send the target asynchronous request based on the response failure type corresponding to the response data. For example, when the response failure type is a server response timeout, the target asynchronous request can be sent again. The specific settings can be made according to the needs and are not limited here.
[0046] The above-mentioned method for processing an asynchronous request, in response to an asynchronous request trigger message, determines the asynchronous request parameters in the asynchronous request trigger message; based on the asynchronous request parameters and the preset asynchronous request global parameters, constructs a target asynchronous request, sends the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving the response data corresponding to the target asynchronous request, calls a pre-registered response interceptor to process the response data to obtain a processing result; if the processing result indicates that the response to the target asynchronous request fails, the response data is processed by the preset first data processing logic. This method does not require developers to write code for different asynchronous requests. By pre-setting global parameters, the target asynchronous request can be constructed in combination with the current asynchronous request method, making the code concise and easy to maintain; developers can also write the processing logic of the interceptor, determine the situation of asynchronous request response failure through the interceptor, and then process the error data. The processing flow is relatively concise and conducive to the stable operation of the system.
[0047] The following embodiment provides a specific method for constructing a target asynchronous request based on asynchronous request parameters and preset asynchronous request global parameters.
[0048] First, a request configuration object needs to be generated based on the asynchronous request parameters and the preset asynchronous request global parameters. The request configuration object usually needs to include all parameters used to generate asynchronous requests, of which some parameters come from the asynchronous request parameters and some parameters come from the asynchronous request global parameters. When the asynchronous request parameters contain parameters that have the same function as a parameter in the asynchronous request global parameters, the corresponding parameters of the asynchronous request global parameters need to be replaced with the parameters in the asynchronous request parameters. Then, the pre-registered request interceptor can be called to build the target asynchronous request based on the request configuration object.
[0049] The above-mentioned request interceptor is similar to the response interceptor, and both are a type of interceptor. Since it is called before the request is issued, it is called a request interceptor. After determining the parameters required for the asynchronous request, the pre-registered request interceptor can be called, and the request configuration object is subjected to a first processing based on the preset second data processing logic through the request interceptor to obtain a processing result; the first processing can be one or more of adding a request header, modifying a request header, and modifying a request configuration object, which can be specifically set according to business needs and is not limited here. After the request interceptor processes the request configuration object, the target asynchronous request can be constructed based on the processing result. In the specific implementation, it is usually necessary to read the parameters in the request configuration object to generate the request header and request body, and finally construct the target asynchronous request.
[0050] Usually, a configuration structure is set for asynchronous request global parameters. Developers or users can generate configuration update instructions through configuration pages or instruction codes. Configuration update instructions usually indicate the parameters that need to be updated and the updated parameter values. In response to the configuration update instruction, a preset configuration interface needs to be called, and then the asynchronous request global parameters are updated based on the configuration update instruction through the configuration interface.
[0051] The following embodiment provides a specific method of invoking a pre-registered response interceptor to process the response data in response to receiving the response data corresponding to the target asynchronous request.
[0052] When the response data is received, the pre-registered response interceptor needs to be called, and then the response status code corresponding to the target asynchronous request is determined by the response interceptor based on the preset third processing logic and the response data; the above third processing logic is usually determined by the developer based on the business logic. The response status code, also known as the HTTP status code, is a three-digit code returned by the server in response to a client request. They are used to indicate the processing status and results of HTTP requests. Each status code has a specific meaning and is used to convey information about the request processing to the client. The above third processing logic can be to extract the response status code from the response data. Different response status codes have corresponding meanings.
[0053] If the response status code is within the preset first status code range, it is determined that the response to the target asynchronous request has failed, and response status text data is generated; the response status text data is used to describe the reason for the response failure. The above-mentioned first status code range is usually outside the range of 200 to 299. At this time, it is also necessary to generate response status text data based on the failure type corresponding to the response status code to explain the reason for the response failure.
[0054] The following embodiment provides a specific method for processing response data by using a preset first data processing logic if the processing result indicates that the response to the target asynchronous request fails.
[0055] After determining that the response has failed, the response data needs to be second processed through the preset first data processing logic. Specifically, the second processing includes at least one of the following: sending the processing results and related data corresponding to the response data to the designated server, so that the designated server performs analysis based on the processing results and related data corresponding to the response data; in order to enable the user to understand the response situation, it is necessary to generate an error message corresponding to the processing result, and display the error message through a preset graphical user interface; in order to further implement asynchronous requests, it is necessary to perform corresponding recovery actions based on the error type indicated by the processing result; the recovery behavior includes resending the target asynchronous request, or generating a user prompt message, and the user prompt message is used to indicate the specified user operation. For example, if the data entered by the user is incorrect, the user needs to be prompted to enter the correct data.
[0056] In the process of generating the error message corresponding to the processing result, you first need to initialize the preset error mapping object; the error mapping object is used to store the mapping relationship between the response failure type and the error message, and the error message is usually a text that can describe the response failure type in plain language. For example, the response failure type is: semantic error, the current request cannot be understood by the server, and the corresponding error message is usually: please change the query keyword; then, based on the response failure reason indicated by the processing result and the error mapping object, determine the error message corresponding to the processing result.
[0057] The present disclosure also provides another method for processing asynchronous requests. Figure 1 In view of the above situation, this method integrates asynchronous requests based on the architecture, aiming to integrate the asynchronous request process to simplify the development work and enhance the readability and maintainability of the code.
[0058] This method encapsulates the underlying asynchronous communication details by building a unified request processing layer, while providing powerful error handling and global configuration management functions. The core concept of this method is to standardize the processing of asynchronous requests through abstraction and encapsulation, so that developers can send requests and receive responses without having to worry about the underlying implementation details. In addition, through centralized error handling and unified configuration management, it can cope with various network states and abnormal situations, ensuring the stability and reliability of the application.
[0059] The implementation of this solution mainly includes the following key parts:
[0060] Configuration management: Provides a configuration system that allows developers to set global request parameters, such as base URL, timeout settings, etc.
[0061] Configuration management is implemented through the configuration layer. The configuration layer has a simple interface to provide services to the outside world, allowing developers to set it once when the application starts, and it will take effect globally. In this way, the configuration layer provides a centralized place to manage the configuration of all asynchronous requests. Developers can set these configurations when the application is initialized, and then when the request is sent, these configurations will be automatically applied to each request. This design simplifies configuration management, so that when the configuration needs to be changed (for example, changing the API server address), it only needs to be updated in one place, without having to modify the settings of each request one by one.
[0062] Request encapsulation: Create a general request function that can handle various types of HTTP requests and format the responses in a unified manner. Encapsulation can hide the underlying implementation details related to asynchronous requests, such as HTTP request construction, sending, and response parsing. Developers only need to specify the basic information of the request, such as URL, method, and data, without having to write lengthy request processing code.
[0063] The request layer is where HTTP requests are actually executed. It uses the information of the configuration layer and the logic of the interception layer to send requests. A class called RequestSender is defined, which represents the request layer and is responsible for actually executing HTTP requests. In this class, the sendRequest method is the core of sending requests. It first gets the global configuration from the configManager and merges it with request-specific parameters (such as HTTP method, URL, and parameters) to create a complete request configuration object. Then, it calls the interceptorBus.applyMiddlewares method, passing this configuration object and the true parameter indicating that this is a request interception. This will trigger all registered request interceptors to execute in sequence, allowing the configuration object to be modified before sending the request, such as adding or modifying request headers, query parameters, etc.
[0064] In the logic of actually sending the request, you need to create and send the HTTP request according to the information in the config object. After the request is sent, you need to capture the response and use the interceptorBus.applyMiddlewares method again (this time passing false as the second parameter, indicating that this is a response interception) to execute all registered response interceptors, allowing the response to be modified or errors to be handled before returning the response to the caller.
[0065] This design makes the request layer highly flexible and configurable, allowing developers to control the request and response processing flow through the configuration layer and interception layer without having to worry about the underlying request sending details.
[0066] Interceptor mechanism: Introduce request and response interceptors to execute specific logic before and after a request is sent and before and after a response is received, such as adding request headers and processing authentication.
[0067] The design of the interception layer allows developers to process requests or responses before they are sent or after they are received. A class called InterceptorBus is defined, which implements the functionality of the interception layer, allowing developers to process them before requests are sent or after responses are received. With this design, the InterceptorBus class provides a flexible mechanism to handle the interception of requests and responses. Developers can easily add their own interceptor logic, such as adding authentication information, logging, modifying request parameters, processing response data, etc. When a request or response is sent, all registered interceptors will be executed in the order they are added, allowing for complex processing flows.
[0068] Unified error handling: Design an error handling mechanism that can capture and uniformly handle various network errors and business logic errors.
[0069] Error handling is implemented through the response layer, which needs to process the response data from the server, apply the response interceptor logic of the interception layer to the response data, and handle possible errors. A class called ResponseHandler is defined, which represents the response layer and is responsible for handling the response from the server. In this class, the handleResponse method is the core of handling the response. It first executes all registered response interceptors, allowing the response to be modified or other logic to be processed before returning the response to the caller.
[0070] Next, the handleResponse method checks the ok property of the response object. This property is a Boolean value that is true when the response status code (determined by the response interceptor) is in the range of 200 to 299, indicating that the request was successful. If ok is false, it means that an error has occurred. At this time, the handleError method of errorHandler is called, and response.statusText (equivalent to the above "response status text data") is passed in as a parameter. response.statusText contains a brief description of why the request failed, for example, "NotFound" or "Internal ServerError".
[0071] Finally, if the response is successful (ok is true), the handleResponse method will attempt to parse the response body as JSON. This is done by calling the json method of the response object, which returns a Promise that contains the response data in JSON format after parsing is complete.
[0072] This design enables the response layer to centrally handle response data parsing and error handling, while providing the ability to execute response interceptor logic, enhancing the modularity and maintainability of the code.
[0073] The error handling mechanism centrally handles all errors that may occur during the request process and provides unified error feedback. A class called ErrorHandler is defined, which represents the error handling mechanism and is responsible for centrally handling all errors that may occur during asynchronous requests and providing unified error feedback. In this class, the constructor method initializes an errorMap object, which is a simple mapping of error types to user-friendly messages. This design allows the error type to be quickly converted into a description that the user can understand when handling errors.
[0074] The handleError method is the core of error handling. It accepts a message parameter, which indicates the type of error that occurred or a brief error description. The method looks for a user-friendly message corresponding to message in errorMap. If a corresponding message is found, it is used; if not found, the default message "An unknown error occurred." is used.
[0075] In actual error handling logic, the handleError method can perform a variety of operations, such as:
[0076] Logging: Output error messages to the console or send them to a remote log management system to facilitate subsequent error analysis and debugging.
[0077] User prompts: Display error messages to users through the application's user interface, such as modal windows or notification bars.
[0078] Error reporting: Send error details and context to an error tracking service, such as Sentry or Rollbar, for easier monitoring and analysis.
[0079] Error recovery: Perform appropriate recovery actions based on the error type, such as automatically retrying the request or prompting the user to perform specific operations.
[0080] This centralized error handling mechanism enables the application to handle various error situations in a unified and consistent manner, improving the robustness of the application and user experience.
[0081] This method provides a comprehensive and integrated solution that not only solves the shortcomings of existing technologies, but also improves the efficiency and effectiveness of asynchronous request processing through innovative architectural design. Based on this solution, developers can focus more on the implementation of business logic without having to get bogged down in the details of asynchronous communication.
[0082] The core concept of this solution is to create a highly abstract asynchronous request processing framework, which aims to provide a concise, consistent and powerful interface to manage asynchronous communication in web applications. The framework will encapsulate all the complexities associated with asynchronous requests, including network communication, error handling, data parsing and request configuration, so that developers can handle asynchronous logic in a declarative way; introduce a unified error handling mechanism that can capture and handle all errors that may occur in the asynchronous request process, including network problems, server errors and client logic errors. This ensures that errors can be identified and responded to in a consistent manner, improving the robustness of the application; and provide a centralized configuration management system that allows developers to set and modify global parameters that affect all asynchronous requests, such as timeouts, request headers, base URLs, etc. This centralized management method simplifies configuration maintenance and ensures consistency of request configuration across applications; implements request and response interceptors, allowing developers to execute custom logic before a request is sent or after a response is received. This facilitates the processing of common tasks across multiple requests, such as user authentication, logging, and request retries.
[0083] This approach adopts a modular design of the framework, which supports on-demand loading and expansion. Developers can add or modify specific functional modules as needed, such as specific data processing plug-ins or customized request strategies, without affecting the core functions of the framework.
[0084] This approach simplifies request processing: by encapsulating the underlying asynchronous request details, developers can easily send requests without writing lengthy code, which reduces the complexity of writing and maintaining request logic.
[0085] This approach provides unified configuration management: centrally managing global configuration of requests, such as base URLs, timeout settings, and request headers, makes it easier to update and maintain these parameters while ensuring global consistency.
[0086] This method provides a powerful interceptor mechanism: request and response interceptors provide a flexible hook that allows developers to execute custom logic before a request is sent or after a response is received, such as adding authentication information, logging, error handling, etc.
[0087] This approach allows for centralized error handling: a unified error handling mechanism allows developers to centrally handle different types of errors, improves the efficiency of error management, and provides users with more consistent feedback.
[0088] This method improves code reusability: through modular design and encapsulated request methods, code reusability is improved, duplicate code is reduced, and the size of the application is reduced; it enhances maintainability: because the request logic is centrally managed and encapsulated, it becomes easier to maintain and update the logic, which helps to improve the long-term maintainability of the project; it improves development efficiency: developers can quickly build and modify requests, which reduces development time and speeds up the development process; it improves user experience: through unified error handling and timely user feedback, it can reduce user confusion and waiting time, thereby improving the overall user experience.
[0089] This approach makes the code easier to test: encapsulated requests and modular design make unit testing and integration testing easier, helping to improve code quality and discover potential errors; it is flexible and extensible, allowing developers to flexibly add new features or modify existing logic based on project requirements, supporting application expansion and growth; it reduces external dependencies: by reducing reliance on third-party libraries, potential compatibility issues and update risks can be avoided, while reducing application loading time; it achieves better version control and collaboration: centralized configuration and modular structure make multi-person collaborative development smoother and simplify the version control and code review process.
[0090] For the above method embodiments, see Figure 2 An asynchronous request processing device is shown, the device comprising:
[0091] The asynchronous request parameter acquisition module 202 is used to obtain the asynchronous request parameters in response to the generation trigger event of the asynchronous request; the asynchronous request parameters are used to indicate at least one of the following: server address, data request mode and related business data;
[0092] The target asynchronous request building module 204 is used to build a target asynchronous request based on the asynchronous request parameters and the preset asynchronous request global parameters, and send the target asynchronous request to the server so that the server returns response data based on the target asynchronous request;
[0093] The interceptor processing module 206 is used to, in response to receiving the response data corresponding to the target asynchronous request, call the pre-registered response interceptor to process the response data and obtain a processing result;
[0094] The response data processing module 208 is configured to process the response data by using a preset first data processing logic if the processing result indicates that the response to the target asynchronous request fails.
[0095] The above-mentioned asynchronous request processing device responds to an asynchronous request trigger message and determines the asynchronous request parameters in the asynchronous request trigger message; constructs a target asynchronous request based on the asynchronous request parameters and preset asynchronous request global parameters, and sends the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving the response data corresponding to the target asynchronous request, calls a pre-registered response interceptor to process the response data and obtain a processing result; if the processing result indicates that the response to the target asynchronous request fails, the response data is processed by the preset first data processing logic. This method does not require developers to write code for different asynchronous requests. By pre-setting global parameters, the target asynchronous request can be constructed in combination with the current asynchronous request, making the code concise and easy to maintain; developers can also write the processing logic of the interceptor, determine the situation of asynchronous request response failure through the interceptor, and then process the error data. The processing flow is relatively concise and conducive to the stable operation of the system.
[0096] The target asynchronous request construction module is also used to: generate a request configuration object based on asynchronous request parameters and preset asynchronous request global parameters; and call a pre-registered request interceptor to construct a target asynchronous request based on the request configuration object.
[0097] The above-mentioned target asynchronous request construction module is also used to: call a pre-registered request interceptor, and perform a first processing on the request configuration object based on a preset second data processing logic through the request interceptor to obtain a processing result; the first processing includes at least one of the following: adding a request header, modifying a request header, and modifying a request configuration object; based on the processing result, construct a target asynchronous request.
[0098] The above device also includes: a parameter updating module, which is used to call a preset configuration interface in response to a configuration updating instruction, and update the asynchronous request global parameters based on the configuration updating instruction through the configuration interface.
[0099] The above-mentioned interceptor processing module is also used to: call a pre-registered response interceptor, and determine the response status code corresponding to the target asynchronous request through the response interceptor based on the preset third processing logic and response data; if the response status code is within the preset first status code range, it is determined that the response to the target asynchronous request has failed, and response status text data is generated; the response status text data is used to describe the reason for the response failure.
[0100] The above-mentioned response data processing module is also used to: perform a second processing on the response data through a preset first data processing logic; the second processing includes at least one of the following: sending the processing results and related data corresponding to the response data to a designated server, so that the designated server performs analysis based on the processing results and related data corresponding to the response data; generating an error message corresponding to the processing result, and displaying the error message through a preset graphical user interface; based on the error type indicated by the processing result, executing a corresponding recovery behavior; the recovery behavior includes resending the target asynchronous request, or generating a user prompt message, and the user prompt message is used to indicate the specified user operation.
[0101] The response data processing module is also used to: initialize a preset error mapping object; the error mapping object is used to store the mapping relationship between the response failure type and the error message; based on the response failure cause indicated by the processing result and the error mapping object, determine the error message corresponding to the processing result.
[0102] This embodiment further provides an electronic device, including a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the above-mentioned asynchronous request processing method, for example:
[0103] In response to an asynchronous request trigger message, determine the asynchronous request parameters in the asynchronous request trigger message; the asynchronous request parameters are used to indicate at least one of the following: server address, data request method and related business data; based on the asynchronous request parameters and preset asynchronous request global parameters, build a target asynchronous request, send the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving the response data corresponding to the target asynchronous request, call a pre-registered response interceptor to process the response data to obtain a processing result; if the processing result indicates that the response to the target asynchronous request fails, process the response data through a preset first data processing logic.
[0104] The above method does not require developers to write code for different asynchronous requests. By pre-setting global parameters, the target asynchronous request can be constructed in combination with the current asynchronous request, making the code concise and easy to maintain. Developers can also write the processing logic of the interceptor, determine the failure of the asynchronous request response through the interceptor, and then process the error data. The processing flow is relatively concise and conducive to the stable operation of the system.
[0105] Optionally, the above step of constructing a target asynchronous request based on asynchronous request parameters and preset asynchronous request global parameters includes: generating a request configuration object based on the asynchronous request parameters and preset asynchronous request global parameters; calling a pre-registered request interceptor to construct the target asynchronous request based on the request configuration object.
[0106] Optionally, the above-mentioned step of calling a pre-registered request interceptor to process the request configuration object includes: calling a pre-registered request interceptor, performing a first processing on the request configuration object based on a preset second data processing logic through the request interceptor, and obtaining a processing result; the first processing includes at least one of the following: adding a request header, modifying a request header, and modifying the request configuration object; based on the processing result, constructing a target asynchronous request.
[0107] Optionally, the method further includes: in response to the configuration update instruction, calling a preset configuration interface, and updating the asynchronous request global parameters based on the configuration update instruction through the configuration interface.
[0108] Optionally, the step of calling a pre-registered response interceptor to process the response data includes: calling a pre-registered response interceptor, and determining, through the response interceptor, a response status code corresponding to the target asynchronous request based on a preset third processing logic and response data; if the response status code is within a preset first status code range, determining that the response to the target asynchronous request has failed, and generating response status text data; the response status text data is used to describe the reason for the response failure.
[0109] Optionally, the above-mentioned step of processing the response data through the preset first data processing logic includes: performing a second processing on the response data through the preset first data processing logic; the second processing includes at least one of the following: sending the processing results and related data corresponding to the response data to a designated server, so that the designated server performs analysis based on the processing results and related data corresponding to the response data; generating an error message corresponding to the processing result, and displaying the error message through a preset graphical user interface; based on the error type indicated by the processing result, executing a corresponding recovery behavior; the recovery behavior includes resending the target asynchronous request, or generating a user prompt message, and the user prompt message is used to indicate the specified user operation.
[0110] Optionally, the above step of generating an error message corresponding to the processing result includes: initializing a preset error mapping object; the error mapping object is used to store the mapping relationship between the response failure type and the error message; based on the response failure cause indicated by the processing result and the error mapping object, determining the error message corresponding to the processing result.
[0111] See also Figure 3 As shown, the electronic device includes a processor 100 and a memory 101. The memory 101 stores machine executable instructions that can be executed by the processor 100. The processor 100 executes the machine executable instructions to implement the above-mentioned asynchronous request processing method.
[0112] Further, Figure 3 The electronic device shown further includes a bus 102 and a communication interface 103 , and the processor 100 , the communication interface 103 and the memory 101 are connected via the bus 102 .
[0113] The memory 101 may include a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk storage. The communication connection between the system network element and at least one other network element is realized through at least one communication interface 103 (which may be wired or wireless), and the Internet, wide area network, local area network, metropolitan area network, etc. may be used. The bus 102 may be an ISA bus, a PCI bus, or an EISA bus, etc. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one bidirectional arrow is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0114] The processor 100 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit or software instructions in the processor 100. The above processor 100 can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components. The methods, steps and logic block diagrams disclosed in the embodiments of the present disclosure can be implemented or executed. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor, etc. The steps of the method disclosed in conjunction with the embodiments of the present disclosure can be directly embodied as a hardware decoding processor for execution, or a combination of hardware and software modules in the decoding processor for execution. The software module may be located in a storage medium mature in the art, such as a random access memory, a flash memory, a read-only memory, a programmable read-only memory, or an electrically erasable programmable memory, a register, etc. The storage medium is located in the memory 101, and the processor 100 reads the information in the memory 101 and completes the steps of the method of the above embodiment in combination with its hardware.
[0115] This embodiment also provides a machine-readable storage medium, which stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the above-mentioned asynchronous request processing method.
[0116] An asynchronous request processing method, device, and electronic device provided in an embodiment of the present disclosure include a computer-readable storage medium storing program code, and the program code includes instructions that can be used to execute the method described in the previous method embodiment, for example:
[0117] In response to an asynchronous request trigger message, determine the asynchronous request parameters in the asynchronous request trigger message; the asynchronous request parameters are used to indicate at least one of the following: server address, data request method and related business data; based on the asynchronous request parameters and preset asynchronous request global parameters, build a target asynchronous request, send the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; in response to receiving the response data corresponding to the target asynchronous request, call a pre-registered response interceptor to process the response data to obtain a processing result; if the processing result indicates that the response to the target asynchronous request fails, process the response data through a preset first data processing logic.
[0118] The above method does not require developers to write code for different asynchronous requests. By pre-setting global parameters, the target asynchronous request can be constructed in combination with the current asynchronous request, making the code concise and easy to maintain. Developers can also write the processing logic of the interceptor, determine the failure of the asynchronous request response through the interceptor, and then process the error data. The processing flow is relatively concise and conducive to the stable operation of the system.
[0119] Optionally, the above step of constructing a target asynchronous request based on asynchronous request parameters and preset asynchronous request global parameters includes: generating a request configuration object based on the asynchronous request parameters and preset asynchronous request global parameters; calling a pre-registered request interceptor to construct the target asynchronous request based on the request configuration object.
[0120] Optionally, the above-mentioned step of calling a pre-registered request interceptor to process the request configuration object includes: calling a pre-registered request interceptor, performing a first processing on the request configuration object based on a preset second data processing logic through the request interceptor, and obtaining a processing result; the first processing includes at least one of the following: adding a request header, modifying a request header, and modifying the request configuration object; based on the processing result, constructing a target asynchronous request.
[0121] Optionally, the method further includes: in response to the configuration update instruction, calling a preset configuration interface, and updating the asynchronous request global parameters based on the configuration update instruction through the configuration interface.
[0122] Optionally, the step of calling a pre-registered response interceptor to process the response data includes: calling a pre-registered response interceptor, and determining, through the response interceptor, a response status code corresponding to the target asynchronous request based on a preset third processing logic and response data; if the response status code is within a preset first status code range, determining that the response to the target asynchronous request has failed, and generating response status text data; the response status text data is used to describe the reason for the response failure.
[0123] Optionally, the above-mentioned step of processing the response data through the preset first data processing logic includes: performing a second processing on the response data through the preset first data processing logic; the second processing includes at least one of the following: sending the processing results and related data corresponding to the response data to a designated server, so that the designated server performs analysis based on the processing results and related data corresponding to the response data; generating an error message corresponding to the processing result, and displaying the error message through a preset graphical user interface; based on the error type indicated by the processing result, executing a corresponding recovery behavior; the recovery behavior includes resending the target asynchronous request, or generating a user prompt message, and the user prompt message is used to indicate the specified user operation.
[0124] Optionally, the above step of generating an error message corresponding to the processing result includes: initializing a preset error mapping object; the error mapping object is used to store the mapping relationship between the response failure type and the error message; based on the response failure cause indicated by the processing result and the error mapping object, determining the error message corresponding to the processing result.
[0125] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0126] In addition, in the description of the embodiments of the present disclosure, unless otherwise clearly specified and limited, the terms "installed", "connected", and "connected" should be understood in a broad sense, for example, it can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection, or it can be indirectly connected through an intermediate medium, or it can be the internal communication of two components. For those skilled in the art, the specific meanings of the above terms in the present disclosure can be understood according to specific circumstances.
[0127] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present disclosure, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present disclosure. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program code.
[0128] In the description of the present disclosure, it should be noted that the terms "center", "upper", "lower", "left", "right", "vertical", "horizontal", "inner", "outer", etc., indicating the orientation or positional relationship, are based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the present disclosure and simplifying the description, rather than indicating or implying that the device or element referred to must have a specific orientation, be constructed and operated in a specific orientation, and therefore cannot be understood as a limitation of the present disclosure. In addition, the terms "first", "second", and "third" are used for descriptive purposes only, and cannot be understood as indicating or implying relative importance.
[0129] Finally, it should be noted that the above embodiments are only specific implementation methods of the present disclosure, which are used to illustrate the technical solutions of the present disclosure, rather than to limit them. The protection scope of the present disclosure is not limited thereto. Although the present disclosure is described in detail with reference to the above embodiments, those skilled in the art should understand that any technician familiar with the technical field can still modify the technical solutions recorded in the above embodiments within the technical scope disclosed in the present disclosure, or can easily think of changes, or make equivalent replacements for some of the technical features therein; and these modifications, changes or replacements do not make the essence of the corresponding technical solutions deviate from the spirit and scope of the technical solutions of the embodiments of the present disclosure, and should be included in the protection scope of the present disclosure. Therefore, the protection scope of the present disclosure should be based on the protection scope of the claims.
Claims
1. A method for processing an asynchronous request, characterized in that: The method comprises: In response to the asynchronous request trigger message, determining an asynchronous request parameter in the asynchronous request trigger message; the asynchronous request parameter is used to indicate at least one of the following: a server address, a data request method, and related service data; Based on the asynchronous request parameters and preset asynchronous request global parameters, construct a target asynchronous request, and send the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; In response to receiving the response data corresponding to the target asynchronous request, calling a pre-registered response interceptor to process the response data to obtain a processing result; If the processing result indicates that the response to the target asynchronous request fails, the response data is processed by a preset first data processing logic.
2. The method according to claim 1, characterized in that The step of constructing a target asynchronous request based on the asynchronous request parameters and preset asynchronous request global parameters includes: Generate a request configuration object based on the asynchronous request parameters and preset asynchronous request global parameters; Calls the pre-registered request interceptor based on the request configuration object, building the target asynchronous request.
3. The method according to claim 2, characterized in that The step of calling a pre-registered request interceptor to process the request configuration object comprises: Calling a pre-registered request interceptor, and performing a first process on the request configuration object based on a preset second data processing logic by the request interceptor to obtain a processing result; the first process includes at least one of the following: adding a request header, modifying a request header, and modifying the request configuration object; Based on the processing result, a target asynchronous request is constructed.
4. The method according to claim 2, characterized in that: The method further comprises: In response to the configuration update instruction, a preset configuration interface is called, and the asynchronous request global parameters are updated based on the configuration update instruction through the configuration interface.
5. According to the method of claim 1, the step of calling a pre-registered response interceptor to process the response data comprises: Calling a pre-registered response interceptor, and determining a response status code corresponding to the target asynchronous request through the response interceptor based on a preset third processing logic and the response data; If the response status code is within a preset first status code range, it is determined that the response to the target asynchronous request has failed, and response status text data is generated; the response status text data is used to describe the reason for the response failure.
6. The method according to claim 1, wherein the step of processing the response data by a preset first data processing logic comprises: Performing a second process on the response data by using a preset first data processing logic; The second process includes at least one of the following: Sending the processing result and related data corresponding to the response data to the designated server, so that the designated server performs analysis based on the processing result and related data corresponding to the response data; Generate an error message corresponding to the processing result, and display the error message through a preset graphical user interface; Based on the error type indicated by the processing result, a corresponding recovery behavior is executed; the recovery behavior includes resending the target asynchronous request, or generating a user prompt message, wherein the user prompt message is used to indicate a specified user operation.
7. The method according to claim 6, characterized in that The step of generating an error message corresponding to the processing result includes: Initialize a preset error mapping object; the error mapping object is used to store the mapping relationship between the response failure type and the error message; Based on the response failure reason indicated by the processing result and the error mapping object, an error message corresponding to the processing result is determined.
8. A device for processing asynchronous requests, characterized in that: The device comprises: An asynchronous request parameter acquisition module, used to obtain asynchronous request parameters in response to a generation trigger event of an asynchronous request; the asynchronous request parameters are used to indicate at least one of the following: a server address, a data request method, and related business data; A target asynchronous request construction module, used to construct a target asynchronous request based on the asynchronous request parameters and preset asynchronous request global parameters, and send the target asynchronous request to the server, so that the server returns response data based on the target asynchronous request; The interceptor processing module is used for, in response to receiving the response data corresponding to the target asynchronous request, calling the pre-registered response interceptor to process the response data and obtain a processing result; The response data processing module is used to process the response data by using a preset first data processing logic if the processing result indicates that the response to the target asynchronous request fails.
9. An electronic device, characterized in that: It comprises a processor and a memory, wherein the memory stores machine executable instructions that can be executed by the processor, and the processor executes the machine executable instructions to implement the asynchronous request processing method according to any one of claims 1 to 7.
10. A machine-readable storage medium, characterized in that: The machine-readable storage medium stores machine-executable instructions. When the machine-executable instructions are called and executed by a processor, the machine-executable instructions prompt the processor to implement the asynchronous request processing method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Fatch-based data communication method and Fatch-based data communication system
CN112667948A
Packaging registration global interception method and device based on axios
CN112905160A
Executing computer instruction including asynchronous operation
US20180137009A1
Cited By
Asynchronous request processing method and device, equipment and storage medium
CN119440673A
Asynchronous IO calling method and device for service codes, storage medium and computer equipment
CN122132127A
Asynchronous I / O call methods, devices, storage media, and computer equipment in business code
CN122132127B
