Asynchronous programming implementation method and device, equipment and storage medium

By using dependency injection objects and promise objects in the asynchronous task nodes, the asynchronous tasks are encapsulated, which solves the problem of complex asynchronous logic in existing asynchronous programming solutions, resulting in low code readability and higher readability and maintainability are achieved.

CN120179258APending Publication Date: 2025-06-20GUANGZHOU KUGOU COMP TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510257047.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-05
Publication Date
2025-06-20

AI Technical Summary

Technical Problem

The asynchronous logic in existing asynchronous programming solutions is relatively large, resulting in low readability of code and difficult to maintain.

Method used

Add dependency injection objects in the asynchronous task node, and use the promise object as a dependency. Control of asynchronous tasks through the external interface of the dependency injection object. Use the promise specification to encapsulate the asynchronous tasks and support chain calls.

Benefits of technology

Improves readability and maintainability of asynchronous code, avoids callback hell, and makes the structure clearer and easier to understand.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120179258A_ABST
    Figure CN120179258A_ABST
Patent Text Reader

Abstract

The invention discloses an asynchronous programming implementation method and device, equipment and a storage medium, and relates to the technical field of computers. The method comprises the following steps: creating an asynchronous task node in an asynchronous programming environment, wherein the asynchronous task node is used for realizing an asynchronous task; a dependency injection object is added in the asynchronous task node, the dependency injection object is used for controlling implementation of the asynchronous task in a mode of injecting a dependency item, and the dependency item of the dependency injection object is a promise object; and creating an external interface of the dependency injection object, wherein the external interface is used for acquiring the dependency injection object to control the asynchronous task. According to the method, the promise object serves as the dependency item to be added to the dependency injection object in the asynchronous task node, so that the structure of the asynchronous code is clearer and easier to understand, compiling of the asynchronous code can be simplified when huge asynchronous logic is achieved, and readability and maintainability of the asynchronous code are enhanced.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular, to a method, apparatus, device, and storage medium for implementing asynchronous programming. Background Art

[0002] Decoupled asynchrony plays an important role in software development and system design, especially when dealing with complex tasks and time-consuming operations.

[0003] In related technologies, programming methods for decoupled asynchrony usually involve mechanisms such as event-driven, callback functions, Promise, Future, CompletableFuture, message queues, etc. The event-driven model is an event-based asynchronous communication method. In the event-driven model, the publisher (producer) of an event communicates with the subscriber (consumer) of the event through the event, achieving loose coupling. Using asynchronous programming patterns such as callback functions, Promise, Future, CompletableFuture, etc. to handle the results of asynchronous tasks can avoid blocking the main thread and improve the responsiveness of the program. A message queue is a middleware for transmitting messages between different systems or components. It allows messages to be sent and received asynchronously, thus achieving decoupling between systems.

[0004] However, the asynchronous logic in the above solutions is relatively large, resulting in low readability of the asynchronous logic code. Summary of the Invention

[0005] Embodiments of this application provide a method, apparatus, device, and storage medium for implementing asynchronous programming. The technical solutions provided by the embodiments of this application are as follows:

[0006] According to one aspect of the embodiments of this application, a method for implementing asynchronous programming is provided. The method includes:

[0007] Create an asynchronous task node in an asynchronous programming environment, where the asynchronous task node is used to implement an asynchronous task;

[0008] Add a dependency injection object to the asynchronous task node, where the dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies, and the dependencies of the dependency injection object are promise objects;

[0009] Create an external interface for the dependency injection object, where the external interface is used to obtain the dependency injection object to implement control over the asynchronous task.

[0010] According to one aspect of the embodiments of this application, an apparatus for implementing asynchronous programming is provided. The apparatus includes:

[0011] A node creation module, configured to create an asynchronous task node in an asynchronous programming environment, where the asynchronous task node is used to implement an asynchronous task;

[0012] A dependency injection module, configured to add a dependency injection object to the asynchronous task node, where the dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies, and the dependencies of the dependency injection object are promise objects;

[0013] An interface creation module, configured to create an external interface of the dependency injection object, where the external interface is used to obtain the dependency injection object to implement control over the asynchronous task.

[0014] According to one aspect of an embodiment of the present application, a computer device is provided. The computer device includes a processor and a memory. A computer program is stored in the memory, and the computer program is loaded and executed by the processor to implement the above-mentioned method for implementing asynchronous programming.

[0015] According to one aspect of an embodiment of the present application, a computer-readable storage medium is provided. A computer program is stored in the computer-readable storage medium, and the computer program is loaded and executed by a processor to implement the above-mentioned method for implementing asynchronous programming.

[0016] According to one aspect of an embodiment of the present application, a computer program product is provided. The computer program product includes a computer program, and the computer program is loaded and executed by a processor to implement the above-mentioned method for implementing asynchronous programming.

[0017] The technical solution provided by the embodiment of the present application can bring the following beneficial effects:

[0018] By adding a promise object as a dependency to the dependency injection object in the asynchronous task node, the asynchronous task can be encapsulated by leveraging the role of the promise specification, making the asynchronous code look more synchronous and intuitive, and enhancing the readability of the asynchronous code. And the promise specification allows for chained calls, enabling multiple asynchronous tasks to be chained together, avoiding the problem of difficult-to-read and maintainable code caused by excessive callback functions resulting in code nesting and stacking, thereby effectively avoiding callback hell and making the structure of the asynchronous code clearer and easier to understand. This enables the simplification of the writing of asynchronous code when implementing relatively large asynchronous logic, enhancing the readability and maintainability of the asynchronous code. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] Figure 1 is a schematic diagram of the implementation environment of the solution provided by an embodiment of the present application;

[0020] Figure 2It is a flowchart of an implementation method of asynchronous programming provided by an embodiment of the present application;

[0021] Figure 3 It is a schematic diagram of an implementation process of asynchronous programming provided by an embodiment of the present application;

[0022] Figure 4 It is a block diagram of an implementation device of asynchronous programming provided by an embodiment of the present application;

[0023] Figure 5 It is a structural block diagram of a computer device provided by an embodiment of the present application. Detailed implementation manners

[0024] To make the objectives, technical solutions, and advantages of the present application clearer, the embodiments of the present application will be further described in detail below with reference to the accompanying drawings.

[0025] Please refer to Figure 1 , which shows a schematic diagram of a solution implementation environment provided by an embodiment of the present application. This solution implementation environment can be implemented as an implementation system of asynchronous programming. This solution implementation environment may include: a terminal device 10 and a server 20.

[0026] The number of terminal devices 10 can be one or more. The terminal device 10 can be an electronic device such as a mobile phone, a tablet computer, a notebook computer, a desktop computer, a game console, an e-book reader, a multimedia playback device, a wearable device, a smart voice interaction device, a smart home appliance, a vehicle-mounted terminal, an aircraft, etc.

[0027] A client of a target application can be installed in the terminal device 10. The target application has a programming function. A user inputs code in the target application, and realizes a programming task through the code, and outputs a return result of the programming task. Exemplarily, the target application is a programming application. Optionally, the target application can be an application that needs to be downloaded and installed, or an application that can be used immediately upon clicking. The present application does not make any limitation thereto.

[0028] The server 20 is used to provide a background service for the client of the target application installed and running in the terminal device 10. For example, the server 20 can be the background server of the above target application. The server 20 can be an independent physical server, or a server cluster composed of multiple servers, or a cloud computing service center. Optionally, the server 20 provides background services for the target applications in multiple terminal devices 10 at the same time. The terminal device 10 and the server 20 can communicate with each other through a network.

[0029] In an embodiment of the present application, after a user inputs code in a target application, a terminal device or a server creates an asynchronous task node in an asynchronous programming environment. The asynchronous task node is used to implement an asynchronous task. A dependency injection object is added to the asynchronous task node. The dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies. The dependency of the dependency injection object is a promise object. The dependency injection object is exposed through an external interface of the dependency injection object, so that the dependency injection object can be obtained through the external interface, thereby realizing the control of the asynchronous task.

[0030] Please refer to Figure 2 , which shows a flowchart of a method for implementing asynchronous programming provided by an embodiment of the present application. The execution subject of each step of this method can be a computer device. This method may include at least one of the following steps 210 to 230:

[0031] Step 210, create an asynchronous task node in an asynchronous programming environment. The asynchronous task node is used to implement an asynchronous task.

[0032] The asynchronous programming environment refers to an environment for implementing programming code that allows a program to continue executing other asynchronous tasks while waiting for certain asynchronous tasks (such as I / O operations, network requests) to complete, rather than blocking the execution of other tasks while waiting for certain tasks to complete, so that the program can only execute other tasks in sequence after waiting for the current task to complete. By creating an asynchronous programming environment, multiple asynchronous tasks can be scheduled and executed simultaneously, improving the response efficiency and performance of the program. It should be noted that the asynchronous programming environment does not limit the programming language, operating system, or specific programming framework.

[0033] An asynchronous task node is a basic building block in an asynchronous programming environment and is a node used to implement an asynchronous task. One or more asynchronous task nodes can be created in an asynchronous programming environment. Each asynchronous task node is respectively used to implement an asynchronous task, and the asynchronous tasks to be implemented by different asynchronous task nodes are different. Exemplarily, if asynchronous task node 1 and asynchronous task node 2 are created in an asynchronous programming environment, asynchronous task node 1 is used to implement asynchronous task 1, and asynchronous task node 2 is used to implement asynchronous task 2, then asynchronous task 1 and asynchronous task 2 are different.

[0034] Each asynchronous task node is independent and can be executed and managed separately. Moreover, each asynchronous task node does not block the task execution of the main thread or the execution of other asynchronous task nodes during the execution process. Asynchronous task nodes are usually managed by an event loop or a task scheduler, and these components are responsible for deciding when to execute which task nodes to optimize resource utilization and improve performance.

[0035] An asynchronous task is an operation unit defined in a program that can run independently. Correspondingly, an asynchronous task node can be understood as a function, method, or coroutine defined in an asynchronous programming environment that can execute independently and may wait for certain operations to complete.

[0036] Step 220: Add a dependency injection object to the asynchronous task node. The dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies, and the dependency of the dependency injection object is a promise object.

[0037] A dependency injection object refers to an instance created through dependency injection. These instances are the instantiation results of dependency injection classes and have received their required dependencies through dependency injection. It can be understood that a dependency injection object is an already instantiated class object that has received the necessary dependencies through dependency injection and can directly use the dependencies in these dependency injection objects to control the implementation of the asynchronous task. Dependency injection objects are usually managed by an external container (such as a Spring container), including their creation, destruction, and management of dependencies, etc.

[0038] When an object needs to use another object to complete its function, this relationship is called dependency. The process of passing the dependency relationship to the object from the outside is called injection. Dependency Injection (DI) is a design pattern whose core idea is to hand over the dependency relationships that originally needed to be explicitly created in the code to an external container for control and management. Specifically here, it means injecting dependencies into the dependency injection object. The main role of dependency injection is to decouple and manage the dependency relationships between various dependencies, reduce the coupling degree between various dependencies, improve the readability and maintainability of the code, facilitate unit testing, and support runtime dynamic configuration. By injecting dependencies, the dependency relationships between various dependencies are explicitly declared, and the external container is responsible for creating and injecting the dependency objects, which makes the code more modular, easier to understand, and expandable.

[0039] One or more dependencies can be injected into a dependency injection object. A dependency refers to other classes or objects that a dependency injection object needs to use during its lifecycle. These dependencies are injected into the dependency injection object to provide part of the functions or services for the implementation of the dependency injection object's functions. The functions or services provided by different dependencies are different. For example, if dependency 1 and dependency 2 are injected into a dependency injection object, then dependency 1 is used to provide function 1 for the implementation of the dependency injection object's function, and dependency 2 is used to provide function 2 for the implementation of the dependency injection object's function, and function 1 and function 2 are different.

[0040] The types of functions provided by dependencies include, but are not limited to, service functions, configuration functions, infrastructure functions, etc. Among them, the dependencies of service functions are classes or objects used to provide specific functions. For example, the dependencies of service functions such as database access services, logging services, and email services. The dependencies of configuration functions are classes or objects used to provide configuration information for an application, such as database connection strings, API (Application Programming Interface) keys. The dependencies of infrastructure functions are classes or objects used to provide basic functions, such as caching, message queues, authentication services, etc.

[0041] Among at least one of the dependencies in the dependency injection object, one or more dependencies are promise objects, and the other dependencies can be any dependencies such as object objects, function functions, etc., which are not limited in this application.

[0042] A promise object is an object constructed using the promise specification. The promise specification provides a concise and powerful way to handle asynchronous tasks and solves some common problems in traditional asynchronous programming patterns, such as callback hell. In the embodiments of this application, the promise object is injected into the dependency injection object in the form of a dependency, so that asynchronous tasks can be encapsulated, making the asynchronous code look more synchronous and intuitive. Developers can chain multiple asynchronous tasks together through chaining calls, thus avoiding the nesting and stacking caused by too many callback functions, that is, avoiding the problem of callback hell, making the structure of the asynchronous code clearer and easier to understand, and enhancing the readability of the code.

[0043] In some embodiments, the dependencies include at least one of an object instance and a function instance. Optionally, if the dependencies only include object instances, then the dependency injection object only includes object instances. Or, if the dependencies only include function instances, then the dependency injection object only includes function instances. Or, if the dependencies include object instances and function instances, then the dependency injection object includes object instances and function instances.

[0044] An object instance is used to inject dependencies into a dependency injection object through object injection. That is, an object instance is an instance of an object passed as a parameter to the dependencies of a dependency injection object. The injection of an object instance reduces the coupling between objects by transferring the dependency relationship from inside the object to the outside. This makes the object more independent and modular, and easier to test and maintain. In unit testing, a mock object can be used to replace the real dependency object, making it easier to control the test environment and verify the behavior of the object. The injection of an object instance makes this replacement simple and straightforward. Moreover, the injection of an object instance allows the dynamic replacement of dependency objects at runtime, enhancing the flexibility and scalability of the application. Dependency injection frameworks usually provide a container to manage the lifecycle and dependencies of objects. Through the injection of object instances, developers can focus more on the implementation of business logic without worrying about details such as object creation and destruction.

[0045] A function instance is used to inject dependencies into a dependency injection object through function injection. That is, a function instance is an instance of a function passed as a parameter to the dependencies of a dependency injection object. The injection of a function instance allows a function to be injected as a dependency into other objects, enabling a callback mechanism where the dependency injection object can call different function implementations at runtime, or implementing the strategy pattern by encapsulating specific algorithms into separate classes and invoking them through an interface, allowing the dependency injection object to select different algorithm implementations at runtime. Also, through the injection of function instances, common functions can be encapsulated in functions and injected into different dependency injection objects when needed, improving code reusability and maintainability. In an event-driven architecture, the injection of function instances can be used to decouple the event publisher and subscriber. By injecting event handling functions, subscribers can be easily added or removed without modifying the existing code.

[0046] The specific dependencies included in the dependency injection object can be referred to in the following embodiments and will not be described in detail here.

[0047] In some embodiments, the dependency injection object includes: an object element, a result element, and a status element. The object element is used to store the dependencies of the dependency injection object; the result element is used to store the result value of an asynchronous task; the status element is used to store the status value of an asynchronous task, and the status value is used to indicate that the asynchronous task is in one of the initial state, success state, and failure state.

[0048] Exemplarily, a promise object includes multiple private elements, namely, an object element, a result element, and a status element. The object element is used to store the dependencies of the dependency injection object, that is, the object element is used to store the promise object, and the object element can be represented as promise. The result element is used to store the result value of the asynchronous task. When the asynchronous task is completed, the result value of the asynchronous task is saved in the result element, so that the result value can be accessed and used at an appropriate time. The result element can be represented as _val. The status element is used to store the status value of the asynchronous task. During the execution of the asynchronous task, the status element can be used to track the current status of the asynchronous task, so that the execution process of the asynchronous task can be managed and monitored according to the status value of the status element. The status element can be represented as _state.

[0049] The status value is used to indicate that the asynchronous task is in one of the initial state, success state, and failure state. The initial state (pending) is used to indicate the state when the promise object is created. It can be understood as a waiting state. In this state, it is impossible to determine whether the final result of the asynchronous task is successful or failed. When a promise object is created, the promise object is in the initial state until the asynchronous task is completed. The success state (resolved) is used to indicate the state when the asynchronous task is successfully completed. For example, when a network request successfully returns data, it enters this state. The failure state (rejected) is used to indicate the state when the asynchronous task fails to complete. For example, a network request fails or the data format is incorrect, which will trigger this state. The status value of the status element can be converted from the initial state to the success state, or from the initial state to the failure state. Whether it is converted from the initial state to the success state or from the initial state to the failure state is determined by the completion result of the asynchronous task. Once the status value of the status element is converted to the success state or the failure state, it cannot be changed anymore, that is, the change of the status value is one-way and irreversible.

[0050] By setting the object element, the result element, and the status element in the promise object, it is possible to determine the execution result and execution status of the asynchronous task according to each private element, so that chained calls can be implemented according to the values of each private element, making the code writing of the asynchronous task more flexible and concise.

[0051] Step 230, create an external interface for the dependency injection object. The external interface is used to obtain the dependency injection object to implement the control of the asynchronous task.

[0052] By creating an external interface for the dependency injection object, the dependency injection object is externally exposed, so that other components or systems can access and use the dependency injection object through the external interface of the dependency injection object, and obtain the dependency injection object to implement the control of the asynchronous task.

[0053] Under normal circumstances, dependency injection objects can be registered in the service container so that other components can look up and use the dependency injection objects through the service container. Alternatively, the service can be published as a remotely callable interface via RESTful API, gRPC, or other communication protocols, so that other systems or components can call the dependency injection objects over the network. Alternatively, a public interface or access point can be provided to allow other components to obtain and use the dependency injection objects through the public interface or access point.

[0054] It should be noted that exposing dependency injection objects externally requires considering security issues, and only authorized users or systems can access and use the dependency injection objects through the external interface of the dependency injection objects.

[0055] In some embodiments, the external interface is used to implement at least one of the following control functions: pause control function for asynchronous tasks; resume control function for asynchronous tasks; end control function for asynchronous tasks; control to obtain intermediate state information of asynchronous tasks.

[0056] The pause control function for asynchronous tasks is used to pause the processing of the current asynchronous task, the resume control function for asynchronous tasks is used to resume the processing of the paused asynchronous task, the end control function for asynchronous tasks is used to end the processing of the current asynchronous task, and obtaining the intermediate state information of asynchronous tasks is used to obtain the intermediate processing state of asynchronous tasks.

[0057] Exemplarily, the asynchronous task manager can be injected into the controller or service through dependency injection, which can ensure that the life cycle and state management of asynchronous tasks are responsible by a unified component. And the current state of the task can be recorded by encapsulating the enumeration type of the task state, and the state can be updated during the execution of the task. The intermediate state information can also be obtained during the execution of the task through a callback mechanism or polling method. Thus, the requirements of asynchronous task management are met.

[0058] The technical solution provided by the embodiments of the present application adds a promise object as a dependency to the dependency injection object in the asynchronous task node, so that the asynchronous task can be encapsulated by using the role of the promise specification, making the asynchronous code look more synchronous and intuitive, and enhancing the readability of the asynchronous code. And the promise specification allows for chained calls, and multiple asynchronous tasks can be chained together to avoid the problem that the code is difficult to read and maintain caused by excessive callback functions resulting in code nesting and stacking, thus effectively avoiding callback hell and making the structure of the asynchronous code clearer and easier to understand. When implementing relatively large asynchronous logic, it can simplify the writing of asynchronous code and enhance the readability and maintainability of the asynchronous code.

[0059] In some embodiments, the dependency injection object only includes an object instance, which is used to inject dependencies into the dependency injection object by means of object injection. The object instance is constructed based on a first function and a second function. After step 230 described above, step 240 is further included, and step 240 includes at least one of sub-steps 241 to 242.

[0060] Sub-step 241, controlling the first function to return the value of the status element.

[0061] The first function is used to return the value of the status element. Exemplarily, the first function is the getState function, and the getState function is used to return the value of the status element (_state).

[0062] Sub-step 242, controlling the second function to return the value of the result element.

[0063] The second function is used to return the value of the result element. Exemplarily, the first function is the getVal function, and the getState function is used to return the value of the result element (_val).

[0064] Exemplarily, in the case where the dependency injection object only includes an object instance, the dependency injection object can be represented as:

[0065]

[0066] this._val = val;

[0067] this._state ='resolved';

[0068] }

[0069] reject() {

[0070] this._state ='rejected';

[0071] }

[0072] }

[0073] By constructing an object instance based on the first function and the second function, the status and result that occur during the execution of the asynchronous task can be intuitively understood, which helps to manage and debug the asynchronous task according to the values of the status element and the result element.

[0074] In some embodiments, the dependency injection object only includes a function instance, which is used to inject dependencies into the dependency injection object by means of function injection. The function instance is constructed based on a third function and a fourth function. After step 230 described above, step 250 is further included, and step 250 includes at least one of sub-steps 251 to 252.

[0075] Sub-step 251: When the value of the result element is set to the input parameter value of the third function and the value of the status element is set to indicate that the asynchronous task is in a successful state, control the third function to return the value of the status element.

[0076] Exemplarily, the third function is the resolve function. The third function is used to change the state of the promise object from the initial state to the successful state and, when the asynchronous task is in the successful state, return the value of the status element. That is, the third function is used to return the value of the status element indicating that the asynchronous task is in the successful state when the asynchronous task is in the successful state. Then, according to the value of the status element returned by the third function, execute the corresponding callback function.

[0077] Therefore, when the third function returns the value of the status element, it can be considered that the asynchronous task is in the successful state.

[0078] Sub-step 252: When the value of the result element is set to the input parameter value of the fourth function and the value of the status element is set to indicate that the asynchronous task is in the failed state, control the fourth function to return the value of the status element.

[0079] Exemplarily, the fourth function is the reject function. The fourth function is used to change the state of the promise object from the initial state to the failed state and, when the asynchronous task is in the failed state, return the value of the status element. That is, the fourth function is used to return the value of the status element indicating that the asynchronous task is in the failed state when the asynchronous task is in the failed state. Then, according to the value of the status element returned by the fourth function, execute the corresponding callback function.

[0080] Therefore, when the fourth function returns the value of the status element, it can be considered that the asynchronous task is in the failed state.

[0081] Exemplarily, when the dependency injection object only includes function instances, the dependency injection object can be represented as:

[0082] class DepInjection{

[0083] constructor(){

[0084] this.promise = new Promise((resolve,reject) =>{

[0085] this._resolve = resolve;

[0086] this._reject = reject;

[0087] };

[0088] }

[0089] resolve(val) {

[0090] this._resolve(val);

[0091] }

[0092] reject(reason) {

[0093] this._reject(reason);

[0094] }

[0095] }

[0096] By constructing a constructor instance based on the third function and the fourth function, it is possible to determine the status of the asynchronous task according to the return values of the third function and the fourth function, so that the management and debugging of the asynchronous task can be carried out according to the return values of the third function and the fourth function. And the values of the third function and the fourth function can be used for chained calls of the callback function to simplify the structure of the asynchronous task and enhance the readability of the asynchronous code.

[0097] In some embodiments, the dependency injection object includes an object instance and a function instance. The object instance is used to inject dependencies into the dependency injection object by means of object injection, and the function instance is used to inject dependencies into the dependency injection object by means of function injection. The object instance is constructed based on the first function and the second function, and the function instance is constructed based on the third function and the fourth function. Then, after step 230, step 260 is further included, and step 260 includes at least one of sub-steps 261 to 264.

[0098] Sub-step 261, controlling the value of the status element returned by the first function.

[0099] Sub-step 262, controlling the value of the result element returned by the second function.

[0100] Sub-step 263, when setting the value of the result element to the input parameter value of the third function and setting the value of the status element to indicate that the asynchronous task is in a successful state, controlling the third function to return the value of the status element.

[0101] Sub-step 264, when setting the value of the result element to the input parameter value of the fourth function and setting the value of the status element to indicate that the asynchronous task is in a failed state, controlling the fourth function to return the value of the status element.

[0102] The specific processes of sub-steps 261 to 264 can refer to the above embodiments and will not be elaborated here.

[0103] Exemplarily, when the dependency injection object includes an object instance and a function instance, the dependency injection object can be expressed as:

[0104]

[0105] this._val = val;

[0106] this._state ='resolved';

[0107] }

[0108] reject(reason) {

[0109] this._reject(reason);

[0110] this._state ='rejected';

[0111] }

[0112] }

[0113] By adopting the dependency injection method of object instances and function instances, the dependency injection object is applicable not only to scenarios that require strict control over the lifecycle and decoupling of dependencies, but also to scenarios that require dynamic change of dependencies or simplification of code structure, thereby further realizing the decoupling between the dependency injection object and the dependencies, improving the flexibility of the application of object instances, and simplifying the structure of code writing.

[0114] It should be noted that the above step 240, step 250, and step 260 are parallel solutions, and the corresponding step is selected according to the instances included in the dependency injection object. When the dependency injection object only includes an object instance, step 240 is executed; when the dependency injection object only includes a function instance, step 250 is executed; when the dependency injection object includes an object instance and a function instance, step 260 is executed.

[0115] In some embodiments, the above method further includes step 270, which is executed after step 220, and step 270 includes at least one of sub-steps 271 to 272.

[0116] Sub-step 271, in the asynchronous task node, create at least one asynchronous task sub-node, and each asynchronous task sub-node is used to asynchronously implement a sub-task of the asynchronous task.

[0117] In an asynchronous task node, creating at least one asynchronous task sub-node is used to divide the asynchronous task to be implemented by the asynchronous task node into at least one sub-task for implementation. At least one sub-task obtained by dividing the asynchronous task by at least one asynchronous task sub-node is implemented by the at least one asynchronous task sub-node, and each asynchronous task sub-node is used to asynchronously implement one sub-task of the asynchronous task. It can be understood that in the asynchronous task node, the first-layer asynchronous task sub-nodes are created.

[0118] The number of asynchronous task sub-nodes depends on the complexity of the asynchronous logic of the asynchronous task. The more complex the asynchronous logic of the asynchronous task is, the more asynchronous task sub-nodes there are; the simpler the asynchronous logic of the asynchronous task is, the fewer asynchronous task sub-nodes there are.

[0119] Exemplarily, if the asynchronous task is divided into sub-task 1, sub-task 2, and sub-task 3, then 3 asynchronous task sub-nodes, namely asynchronous task sub-node 1, asynchronous task sub-node 2, and asynchronous task sub-node 3, are created in the asynchronous task node. Among them, asynchronous task sub-node 1 is used to asynchronously implement sub-task 1, asynchronous task sub-node 2 is used to asynchronously implement sub-task 2, and asynchronous task sub-node 3 is used to asynchronously implement sub-task 3.

[0120] Sub-step 272: For each asynchronous task sub-node among the at least one asynchronous task sub-node, add a dependency injection sub-object in the asynchronous task sub-node. The dependency injection sub-object is used to control the implementation of the sub-task by injecting dependencies. The dependency of the dependency injection sub-object is a promise object or an object other than the promise object.

[0121] The dependency injection sub-object is an instance created by the method of dependency injection. At least one sub-dependency is injected into the dependency injection sub-object. It can be understood that the first-layer dependency injection sub-object is added in the asynchronous task sub-node. Here, the creation method of the dependency injection sub-object is the same as the creation method of the above-mentioned dependency injection object. However, in the dependency injection sub-object, the sub-dependency can be a promise object or not, such as a dependency like an object object or a function function. When the sub-dependency of the dependency injection sub-object is a promise object, compared with using other sub-dependencies, it can make the structure of the asynchronous code clearer and easier to understand, simplify the writing of the asynchronous code, and enhance the readability and maintainability of the asynchronous code.

[0122] Optionally, all of the at least one sub-dependency can only include object instances, or can only include function instances, or there can be one or more sub-dependencies that include both object instances and function instances, specifically depending on the requirements of the task to be implemented by the sub-dependency.

[0123] By creating at least one asynchronous task sub-node in the asynchronous task node and adding dependency injection sub-objects in the asynchronous task sub-node, the implementation of the asynchronous task is distributed to each asynchronous task sub-node, and the sub-tasks are respectively implemented by the dependency injection sub-objects, making the asynchronous logic clearer when writing asynchronous code and the structure of the asynchronous code easier to understand, enhancing the readability and maintainability of the asynchronous code.

[0124] In some embodiments, when the asynchronous logic of the asynchronous task is relatively large, issuing only one layer of asynchronous task sub-nodes to the asynchronous task node is not sufficient to clarify the asynchronous logic and structure of the asynchronous task. At least one second-layer asynchronous task sub-node can be created in the first-layer asynchronous task sub-node, and a second-layer dependency injection sub-object can be added to each second-layer asynchronous task sub-node, thereby controlling the implementation of the asynchronous task through a multi-layer structure.

[0125] Figure 3 A schematic diagram showing the implementation process of asynchronous programming is shown. First, a dependency injection object is declared, and private elements are declared in this dependency injection object, including an object element (promise), a result element (_val), and a state element (_state). In this dependency injection object, a getState function is defined to return the value of _state. _state has three possible values: the initial state is pending, the result success state is resolved, and the result failure state is rejected. In this dependency injection object, a getVal function is defined to return the value of _val. In this dependency injection object, a resolve function is defined, and the input parameter is a value of any type. This function sets _val to the input parameter value and sets _state to the success value resolved. In this dependency injection object, a reject function is defined, and the input parameter is a value of any type. This function sets _val to the input parameter value and sets _state to the failure value rejected. In the environment of asynchronous programming, an asynchronous programming context is declared, and the asynchronous programming context contains an asynchronous task node, and a dependency injection object (promise object) is added to the asynchronous task node. By creating an external interface of the dependency injection object, this promise object is externally exposed, enabling the control of the asynchronous logic context to be obtained in any independent asynchronous programming context, including control functions such as pausing / resuming the asynchronous task, ending the asynchronous task, and obtaining intermediate state information of the asynchronous task.

[0126] The following is an apparatus embodiment of the present application, which can be used to execute the method embodiment of the present application. For details not disclosed in the apparatus embodiment of the present application, please refer to the method embodiment of the present application.

[0127] Please refer toFigure 4 , which shows a block diagram of an implementation device for asynchronous programming provided by an embodiment of the present application. The device has the function of implementing the above-mentioned asynchronous programming implementation method, and the function can be implemented by hardware or by hardware executing corresponding software. The device can be the computer device introduced above or can be provided in the computer device. As Figure 4 shown, the device 400 may include: a node creation module 410, a dependency injection module 420, and an interface creation module 430.

[0128] The node creation module 410 is used to create asynchronous task nodes in an asynchronous programming environment, and the asynchronous task nodes are used to implement asynchronous tasks.

[0129] The dependency injection module 420 is used to add a dependency injection object to the asynchronous task node. The dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies, and the dependency of the dependency injection object is a promise object.

[0130] The interface creation module 430 is used to create an external interface for the dependency injection object, and the external interface is used to obtain the dependency injection object to implement the control of the asynchronous task.

[0131] In some embodiments, the dependency injection object includes: an object element, a result element, and a status element;

[0132] The object element is used to store the dependencies of the dependency injection object;

[0133] The result element is used to store the result value of the asynchronous task;

[0134] The status element is used to store the status value of the asynchronous task, and the status value is used to indicate that the asynchronous task is in one of the initial state, success state, and failure state.

[0135] In some embodiments, the dependency injection object includes an object instance. The object instance is used to inject the dependencies into the dependency injection object by object injection, and the object instance is constructed based on a first function and a second function; the device 400 further includes a result return module, and the result return module is used to:

[0136] Control the first function to return the value of the status element;

[0137] Control the second function to return the value of the result element.

[0138] In some embodiments, the dependency injection object includes function instances, which are used to inject the dependencies into the dependency injection object by means of function injection, and the function instances are constructed based on a third function and a fourth function; the result return module is configured to:

[0139] When the value of the result element is set to the input parameter value of the third function and the value of the status element is set to indicate that the asynchronous task is in the success state, control the third function to return the value of the status element;

[0140] When the value of the result element is set to the input parameter value of the fourth function and the value of the status element is set to indicate that the asynchronous task is in the failure state, control the fourth function to return the value of the status element.

[0141] In some embodiments, the dependency injection object includes object instances and function instances, the object instances are used to inject the dependencies into the dependency injection object by means of object injection, the function instances are used to inject the dependencies into the dependency injection object by means of function injection, the object instances are constructed based on a first function and a second function, and the function instances are constructed based on a third function and a fourth function; the result return module is configured to:

[0142] Control the first function to return the value of the status element;

[0143] Control the second function to return the value of the result element;

[0144] When the value of the result element is set to the input parameter value of the third function and the value of the status element is set to indicate that the asynchronous task is in the success state, control the third function to return the value of the status element;

[0145] When the value of the result element is set to the input parameter value of the fourth function and the value of the status element is set to indicate that the asynchronous task is in the failure state, control the fourth function to return the value of the status element.

[0146] In some embodiments, the external interface is used to implement at least one of the following control functions:

[0147] Pause control function for the asynchronous task;

[0148] Resume control function for the asynchronous task;

[0149] End control function for the asynchronous task;

[0150] Control to obtain the intermediate state information of the asynchronous task.

[0151] In some embodiments, the dependency injection module 420 is further configured to:

[0152] In the asynchronous task node, create at least one asynchronous task sub-node, and each asynchronous task sub-node is used to asynchronously implement a sub-task of the asynchronous task;

[0153] For each asynchronous task sub-node among the at least one asynchronous task sub-node, add a dependency injection sub-object in the asynchronous task sub-node, and the dependency injection sub-object is used to control the implementation of the sub-task by injecting dependencies. The dependencies of the dependency injection sub-object are the promise object or other objects other than the promise object.

[0154] It should be noted that when the device provided in the above embodiments realizes its functions, only the division of the above function modules is used for illustration. In actual applications, the above functions can be allocated to different function modules according to needs, that is, the content structure of the device is divided into different function modules to complete all or part of the functions described above. In addition, the device provided in the above embodiments and the method embodiments belong to the same concept, and the specific implementation process can be seen in the method embodiments, which will not be repeated here.

[0155] Please refer to Figure 5 , which shows a structural block diagram of a computer device 500 provided in an embodiment of the present application. The computer device 500 can be any electronic device with data calculation, processing, and storage functions. The computer device 500 can be used to implement the asynchronous programming implementation method provided in the above embodiments.

[0156] Generally, the computer device 500 includes: a processor 501 and a memory 502.

[0157] The processor 501 may include one or more processing cores, such as a quad-core processor, an octa-core processor, etc. The processor 501 may be implemented in at least one of the following hardware forms: DSP (Digital Signal Processing), FPGA (Field Programmable Gate Array), and PLA (Programmable Logic Array). The processor 501 may also include a main processor and a coprocessor. The main processor is a processor used to process data in the wake state, also known as the CPU (Central Processing Unit); the coprocessor is a low-power processor used to process data in the standby state. In some embodiments, the processor 501 may be integrated with a GPU (Graphics Processing Unit), and the GPU is responsible for rendering and drawing the content to be displayed on the display screen. In some embodiments, the processor 501 may further include an AI (Artificial Intelligence) processor, which is used to process computational operations related to machine learning.

[0158] The memory 502 may include one or more computer-readable storage media, and the computer-readable storage media may be non-transitory. The memory 502 may also include high-speed random access memory and non-volatile memory, such as one or more disk storage devices and flash storage devices. In some embodiments, the non-transitory computer-readable storage media in the memory 502 is used to store a computer program, and the computer program is configured to be executed by one or more processors to implement the above-mentioned implementation method of asynchronous programming.

[0159] Those skilled in the art can understand that Figure 5 the structure shown in does not constitute a limitation on the computer device 500, and it may include more or fewer components than shown in the figure, or combine certain components, or adopt a different component layout.

[0160] In a schematic embodiment, a computer-readable storage medium is further provided. A computer program is stored in the storage medium, and when the computer program is executed by the processor of the computer device, the above-mentioned implementation method of asynchronous programming is implemented. Optionally, the above-mentioned computer-readable storage medium may be a ROM (Read-Only Memory), a RAM (Random Access Memory), a CD-ROM (Compact Disc Read-Only Memory), a magnetic tape, a floppy disk, an optical data storage device, etc.

[0161] In an exemplary embodiment, a computer program product is further provided. The computer program product includes a computer program stored in a computer-readable storage medium. A processor of a computer device reads the computer program from the computer-readable storage medium, and the processor executes the computer program, so that the computer device executes the above-mentioned implementation method of asynchronous programming.

[0162] It should be noted that, before and during the process of collecting relevant data of the user, this application can display a prompt interface, a pop-up window or output a voice prompt message. The prompt interface, pop-up window or voice prompt message is used to prompt the user that their relevant data is being collected currently, so that this application only starts to execute the relevant steps of obtaining the user's relevant data after obtaining the confirmation operation sent by the user for the prompt interface or the pop-up window. Otherwise (that is, when the confirmation operation sent by the user for the prompt interface or the pop-up window is not obtained), the relevant steps of obtaining the user's relevant data are ended, that is, the relevant data of the user is not obtained. In other words, all user data collected by this application is processed strictly in accordance with the requirements of relevant national laws and regulations. Obtaining the informed consent or separate consent of the personal information subject is carried out under the condition that the user agrees and authorizes, and subsequent data use and processing behaviors are carried out within the scope of laws and regulations and the authorization of the personal information subject. The collection, use and processing of relevant user data need to comply with the relevant laws, regulations and standards of relevant countries and regions.

[0163] It should be understood that "a plurality of" mentioned herein refers to two or more. "And / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A exists alone, A and B exist simultaneously, and B exists alone. The character " / " generally represents an "or" relationship between the front and rear associated objects. In addition, the step numbers described herein only exemplarily show a possible execution sequence between steps. In some other embodiments, the above steps may not be executed in the order of the numbers. For example, two steps with different numbers are executed simultaneously, or two steps with different numbers are executed in the reverse order of the illustration. The embodiments of this application do not make any limitations in this regard.

[0164] The above are only exemplary embodiments of this application, and are not intended to limit this application. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of this application shall be included in the protection scope of this application.

Claims

1. A method for implementing asynchronous programming, characterized in that: The method comprises: Creating an asynchronous task node in an asynchronous programming environment, wherein the asynchronous task node is used to implement an asynchronous task; Adding a dependency injection object in the asynchronous task node, wherein the dependency injection object is used to control the implementation of the asynchronous task by injecting dependencies, and the dependency of the dependency injection object is a promise object; An external interface of the dependency injection object is created, where the external interface is used to obtain the dependency injection object to achieve control over the asynchronous task.

2. The method according to claim 1, characterized in that The dependency injection object includes: object element, result element and state element; The object element is used to store the dependencies of the dependency injection object; The result element is used to store the result value of the asynchronous task; The state element is used to store the state value of the asynchronous task, and the state value is used to indicate that the asynchronous task is in one of an initial state, a success state, and a failure state.

3. The method according to claim 2, characterized in that The dependency injection object includes an object instance, and the object instance is used to inject the dependency into the dependency injection object by object injection, and the object instance is constructed based on the first function and the second function; The method further comprises: Controlling the first function to return the value of the state element; Control the second function to return the value of the result element.

4. The method according to claim 2, characterized in that: The dependency injection object includes a function instance, and the function instance is used to inject the dependency into the dependency injection object by means of function injection, and the function instance is constructed based on the third function and the fourth function; the method further includes: In a case where the value of the result element is set to the input parameter value of the third function and the value of the status element is set to indicate that the asynchronous task is in the success state, controlling the third function to return the value of the status element; In a case where the value of the result element is set as the input parameter value of the fourth function and the value of the status element is set to indicate that the asynchronous task is in the failed state, the fourth function is controlled to return the value of the status element.

5. The method according to claim 2, characterized in that: The dependency injection object includes an object instance and a function instance, the object instance is used to inject the dependency into the dependency injection object by object injection, the function instance is used to inject the dependency into the dependency injection object by function injection, the object instance is constructed based on the first function and the second function, and the function instance is constructed based on the third function and the fourth function; the method also includes: Controlling the first function to return the value of the state element; Controlling the second function to return the value of the result element; In a case where the value of the result element is set to the input parameter value of the third function and the value of the status element is set to indicate that the asynchronous task is in the success state, controlling the third function to return the value of the status element; In a case where the value of the result element is set as the input parameter value of the fourth function and the value of the status element is set to indicate that the asynchronous task is in the failed state, the fourth function is controlled to return the value of the status element.

6. The method according to claim 1, characterized in that The external interface is used to implement at least one of the following control functions: A pause control function for the asynchronous task; A recovery control function for the asynchronous task; A termination control function for the asynchronous task; Control obtaining the intermediate state information of the asynchronous task.

7. The method according to claim 1, characterized in that The method further comprises: In the asynchronous task node, at least one asynchronous task sub-node is created, each asynchronous task sub-node is used to asynchronously implement a sub-task of the asynchronous task; For each asynchronous task sub-node of the at least one asynchronous task sub-node, a dependency injection sub-object is added in the asynchronous task sub-node, and the dependency injection sub-object is used to control the implementation of the sub-task by injecting dependencies, and the dependency of the dependency injection sub-object is the promise object, or other objects except the promise object.

8. An implementation device for asynchronous programming, characterized in that: The device comprises: A node creation module, used to create an asynchronous task node in an asynchronous programming environment, wherein the asynchronous task node is used to implement an asynchronous task; A dependency injection module, used for adding a dependency injection object in the asynchronous task node, wherein the dependency injection object is used for controlling the implementation of the asynchronous task by injecting dependencies, and the dependency of the dependency injection object is a promise object; An interface creation module is used to create an external interface of the dependency injection object, and the external interface is used to obtain the dependency injection object to achieve control of the asynchronous task.

9. A computer device, characterized in that: The computer device includes a processor and a memory, wherein a computer program is stored in the memory, and the computer program is loaded and executed by the processor to implement the asynchronous programming implementation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, which is loaded and executed by a processor to implement the asynchronous programming implementation method according to any one of claims 1 to 7.

11. A computer program product, characterized in that The computer program product comprises a computer program, and the computer program is loaded and executed by a processor to implement the asynchronous programming implementation method according to any one of claims 1 to 7.