A method and apparatus for dynamically adding asynchronous and returning results of the last added asynchronous

By constructing closures and defining public arrays and public asynchronous operations, dynamically adding and locking promise asynchronous operations, the problem of disordered asynchronous results in JavaScript is solved, ensuring that the last asynchronous result is returned, simplifying data processing logic and avoiding code coupling.

CN115373647BActive Publication Date: 2026-07-24VTION ANZHUO TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
VTION ANZHUO TECH CO LTD
Filing Date
2022-07-11
Publication Date
2026-07-24

AI Technical Summary

Technical Problem

In existing ES6 JavaScript, when multiple asynchronous promises are executed sequentially, the final returned result may not be the result of the last asynchronous promise executed, leading to erroneous data or erroneous data accumulation. Furthermore, existing methods result in code coupling and impure processing logic.

Method used

By constructing closures and defining public arrays and public asynchronous operations, promise asynchronous operations are dynamically added and locked, ensuring that the last asynchronous result is bridged to the public asynchronous operation and the result is returned through the public asynchronous operation.

Benefits of technology

It ensures the return of the asynchronous result of the last promise without adding extra code logic, simplifying data processing logic and avoiding code coupling.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN115373647B_ABST
    Figure CN115373647B_ABST
Patent Text Reader

Abstract

The application discloses a method and device for dynamically adding asynchronous and returning the result of the last added asynchronous, and relates to the technical field of computers. The method adds and locks the promise asynchronous to be executed in sequence by calling a method through constructing a closure and defining a public array and a public asynchronous, so as to return the result of the last added promise asynchronous. The developer only needs to probe whether the variable for storing the closure exists every time a new promise asynchronous needs to be executed in sequence; if the variable exists, the promise asynchronous to be executed is added to the method in sequence by calling the add method on the variable for storing the method; if the variable does not exist, the closure is initialized and constructed and then the add method is called; after the public asynchronous result is returned, the variable for storing the method is emptied, and the asynchronous result processing logic is entered uniformly, without the need to increase any other code logic.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a method and apparatus for dynamically adding asynchronous operations and returning the final result of adding asynchronous operations. Background Technology

[0002] In existing ES6 JavaScript, there are often use cases where multiple asynchronous promises are executed sequentially, and only the result returned by the last asynchronous promise is needed (e.g., a user continuously flips through pages to retrieve data). However, since the execution time of each asynchronous promise is not fixed, the order in which the asynchronous results are returned will not be the same as the execution order. The last result returned may not be the last asynchronous promise executed, which may lead to incorrect data or a backlog of incorrect data.

[0003] Because ES6 JavaScript's promise asynchronous operations cannot be forcibly stopped, the built-in `promise.race` and `promise.all` functions cannot solve this problem. Furthermore, the asynchronous operations they receive execute only once with fixed parameters, preventing continuous addition of new asynchronous operations. A conventional workaround is to add a data lock at the processing point after the promise asynchronous operation returns data, then compare the data to see if it's from the last asynchronous operation (e.g., by comparing timestamps, data IDs, hashes, etc.). If it is, lock the data lock, preventing unnecessary asynchronous operations that haven't yet returned results from processing their data after the return.

[0004] Since the asynchronous subsequent result processing logic in the business logic may be different for each case, this approach not only requires assigning a different data lock and comparison variable to each subsequent result processing logic, but also causes code coupling because the data lock is written into the processing logic, which is not conducive to the purity of the data processing logic. Summary of the Invention

[0005] The technical problem to be solved by the present invention is to provide a method and apparatus for dynamically adding asynchronous operations and returning the result of the last added asynchronous operation. By constructing a closure and defining a public array and a public asynchronous operation, the asynchronous promises to be executed are added and locked sequentially by calling the method, thereby realizing the return of the result of the last added asynchronous promise.

[0006] In a first aspect, the present invention provides a method for dynamically adding asynchronous operations and returning the last added asynchronous result, comprising: a closure construction process and an asynchronous execution process;

[0007] The closure construction process is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronouss, and defines a public asynchronous method belonging to the closure as a bridge for the state of the last passed-in promise asynchronous. Whenever a promise asynchronous to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous execution is completed as ending the public asynchronous. That is, the state of this promise asynchronous is bridged to the public asynchronous. Simultaneously, a data lock is added to this promise asynchronous and the data lock of the last promise asynchronous in the array is locked. Finally, this promise asynchronous is added to the public array, and both the public asynchronous method and the `add` method are returned.

[0008] The asynchronous execution process includes: when there are asynchronous promises that need to be executed sequentially, checking whether the variable storing the closure exists; if it exists, calling the add method on the variable storing the closure in sequence for the asynchronous promises that need to be executed; if it does not exist, executing the closure construction process, returning a public asynchronous array and the add method after construction, then calling the add method to add the asynchronous promises to the public array and bridging the state to the public asynchronous array; then clearing the variable storing the closure after waiting for the public asynchronous array to return the result.

[0009] Furthermore, the add method supports chained calls, where each newly added promise asynchronous operation to be executed is added sequentially to the public asynchronous operation in the public array via the add method.

[0010] Furthermore, the public asynchronous operation is a promise asynchronous operation. After the last added promise asynchronous operation is completed, the public asynchronous operation only returns the result of the last promise asynchronous operation added to the method.

[0011] Secondly, the present invention provides an apparatus for dynamically adding asynchronous operations and returning the last added asynchronous result, comprising: a closure construction module and an asynchronous execution module;

[0012] The closure construction module is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronous operations, and defines a public asynchronous operation belonging to the closure as a bridge for the state of the last passed-in promise asynchronous operation. Whenever a promise asynchronous operation to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous operation is completed as ending the public asynchronous operation. That is, the state of this promise asynchronous operation is bridged to the public asynchronous operation. Simultaneously, a data lock is added to this promise asynchronous operation, and the data lock of the last promise asynchronous operation in the array is locked. Finally, this promise asynchronous operation is added to the public array, and both the public asynchronous operation and the `add` method are returned.

[0013] The asynchronous execution module is used to check whether the variable storing the closure exists when there are asynchronous promises that need to be executed sequentially. If it exists, the add method on the variable storing the closure is called sequentially for each asynchronous promise to be executed. If it does not exist, the closure construction module is executed. After construction, a public asynchronous array and the add method are returned. The asynchronous execution module then calls the add method to add the asynchronous promise to the public array and bridges the state to the public asynchronous array. After waiting for the public asynchronous array to return the result, the variable storing the closure is cleared.

[0014] Furthermore, the add method supports chained calls, where each newly added promise asynchronous operation to be executed is added sequentially to the public asynchronous operation in the public array via the add method.

[0015] Furthermore, the public asynchronous operation is a promise asynchronous operation. When the last added promise asynchronous operation is completed, the public asynchronous operation only returns the result of the last promise asynchronous operation added to the method.

[0016] This invention has at least the following technical effects or advantages:

[0017] By constructing a closure and defining a public array and a public asynchronous function, the asynchronous promises to be executed are added and locked sequentially by calling the method. This ensures that the state bridged to the public asynchronous function is always the last asynchronous promise in the array, and the public asynchronous function will always return the result of the last asynchronous promise added to the method. After waiting for the result of the public asynchronous function to return, the variable storing this method is cleared, and then the asynchronous result processing logic can be entered uniformly without adding any other code logic.

[0018] The above description is merely an overview of the technical solution of the present invention. In order to better understand the technical means of the present invention and to implement it in accordance with the contents of the specification, and in order to make the above and other objects, features and advantages of the present invention more apparent and understandable, specific embodiments of the present invention are described below. Attached Figure Description

[0019] The present invention will be further described below with reference to the accompanying drawings and embodiments.

[0020] Figure 1 This is a flowchart of the method in Embodiment 1 of the present invention;

[0021] Figure 2 This is a schematic diagram of the device in Embodiment 2 of the present invention. Detailed Implementation

[0022] This invention provides a method and apparatus for dynamically adding asynchronous operations and returning the result of the last added asynchronous operation. By constructing a closure and defining a public array and a public asynchronous operation, the asynchronous promises to be executed are added and locked sequentially by calling the method, thereby returning the result of the last added asynchronous promise without adding any other code logic.

[0023] The overall concept of the technical solution in this application is as follows:

[0024] 1. Closure construction

[0025] JavaScript closures are a feature that allows anonymous methods returned by other methods to retain the original method's scope, preventing its destruction after the original method call. This feature can be used to construct methods that can be called at any time, adding asynchronous promise members and remembering all asynchronous members, while also having an internal locking state and preventing them from being destroyed.

[0026] 2. Internal state

[0027] The closure defines an array (hereinafter referred to as the public array) to record all passed-in promises. Due to the existence of the closure, this array is retained after the initialization of the method containing the closure is completed. A separate asynchronous promise belonging to this closure (hereinafter referred to as the public asynchronous promise) is defined as a bridge to the last asynchronous state among all the asynchronous promises passed to this method. Whenever an asynchronous promise to be executed is passed in, the subsequent logic after the execution of this asynchronous promise is defined as ending the public asynchronous promise through a method defined on the public array (the exposed one is called the `add` method). That is, the state of this asynchronous promise is bridged to the public asynchronous promise. Simultaneously, a data lock is acquired for this asynchronous promise, and the data lock of the last asynchronous promise in the array is also locked. Finally, this asynchronous promise is added to the public array, and both the public asynchronous promise and the `add` method are returned. This ensures that the state bridged to the public asynchronous promise is always the last asynchronous promise in the array (i.e., the last asynchronous promise added). The existence of the closure also determines that when the public asynchronous promise is returned, all the data generated in the preceding process will still be executed normally after the `add` method finishes running. The add method can be called again at any time to dynamically add and bridge states.

[0028] 3. External calls

[0029] This scheme ultimately forms the method described above (hereinafter referred to as "this method"). After initialization, this method returns a new public asynchronous promise belonging to this method and an `add` method. The `add` method supports chained calls. After adding the asynchronous promises that need to be executed through the `add` method, it will return the public asynchronous promise mentioned above and a new `add` method. If there are one or more asynchronous promises that need to be executed sequentially during the previous asynchronous execution, then the above steps can be repeated for each one. The public asynchronous promise will always only return the result of the last asynchronous promise added to the method.

[0030] Execution result:

[0031] Developers only need to check if the variable storing this closure method exists each time a new asynchronous promise needs to be executed sequentially. If it exists, then, as described above, add the asynchronous promises to be executed by calling the add method on the variable storing this closure method in sequence. If it does not exist, then initialize and build it as described above, and then call the add method as before. After waiting for the public asynchronous result to return, clear the variable storing this closure method, and then uniformly enter the asynchronous result processing logic without adding any other code logic.

[0032] Example 1

[0033] This embodiment provides a method for dynamically adding asynchronous operations and returning the last added asynchronous result, such as... Figure 1 As shown, this includes: the closure construction process and the asynchronous execution process;

[0034] The closure construction process is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronouss, and defines a public asynchronous method belonging to the closure as a bridge for the state of the last passed-in promise asynchronous. Whenever a promise asynchronous to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous execution is completed as ending the public asynchronous. That is, the state of this promise asynchronous is bridged to the public asynchronous. Simultaneously, a data lock is added to this promise asynchronous and the data lock of the last promise asynchronous in the array is locked. Finally, this promise asynchronous is added to the public array, and both the public asynchronous method and the `add` method are returned.

[0035] The asynchronous execution process includes: when there are asynchronous promises that need to be executed sequentially, checking whether the variable storing the closure exists; if it exists, calling the add method on the variable storing the closure in sequence for the asynchronous promises that need to be executed; if it does not exist, executing the closure construction process, returning a public asynchronous array and the add method after construction, then calling the add method to add the asynchronous promises to the public array and bridging the state to the public asynchronous array; then clearing the variable storing the closure after waiting for the public asynchronous array to return the result.

[0036] The add method supports chained calls. Each newly added promise that needs to be executed is added to the public array before the public promise in turn through the add method.

[0037] The public asynchronous method is a promise asynchronous method. After the last added promise asynchronous method is executed, the public asynchronous method only returns the result of the last promise asynchronous method added to the method.

[0038] Based on the same inventive concept, this application also provides an apparatus corresponding to the method in Embodiment 1, as detailed in Embodiment 2.

[0039] Example 2

[0040] This embodiment provides a device for dynamically adding asynchronous operations and returning the last added asynchronous result, such as... Figure 2As shown, it includes: a closure building module and an asynchronous execution module;

[0041] The closure construction module is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronous operations, and defines a public asynchronous operation belonging to the closure as a bridge for the state of the last passed-in promise asynchronous operation. Whenever a promise asynchronous operation to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous operation is completed as ending the public asynchronous operation. That is, the state of this promise asynchronous operation is bridged to the public asynchronous operation. Simultaneously, a data lock is added to this promise asynchronous operation, and the data lock of the last promise asynchronous operation in the array is locked. Finally, this promise asynchronous operation is added to the public array, and both the public asynchronous operation and the `add` method are returned.

[0042] The asynchronous execution module is used to check whether the variable storing the closure exists when there are asynchronous promises that need to be executed sequentially. If it exists, the add method on the variable storing the closure is called sequentially for each asynchronous promise to be executed. If it does not exist, the closure construction module is executed. After construction, a public asynchronous array and the add method are returned. The asynchronous execution module then calls the add method to add the asynchronous promise to the public array and bridges the state to the public asynchronous array. After waiting for the public asynchronous array to return the result, the variable storing the closure is cleared.

[0043] The add method supports chained calls. Each newly added promise that needs to be executed is added to the public array before the public promise in turn through the add method.

[0044] The public asynchronous method is a promise asynchronous method. After the last added promise asynchronous method is executed, the public asynchronous method only returns the result of the last promise asynchronous method added to the method.

[0045] Since the apparatus described in Embodiment 2 of the present invention is an apparatus used to implement the method of Embodiment 1 of the present invention, those skilled in the art can understand the specific structure and variations of the apparatus based on the method described in Embodiment 1 of the present invention, and therefore will not be described again here. All apparatuses used in the method of Embodiment 1 of the present invention fall within the scope of protection of the present invention.

[0046] This invention constructs a closure and defines a public array and a public asynchronous function. The asynchronous promises to be executed are added and locked sequentially by calling methods. This ensures that the state bridged to the public asynchronous function is always the last asynchronous promise in the array, and the public asynchronous function always returns only the result of the last asynchronous promise added to the method. After waiting for the result of the public asynchronous function to return, the variable storing this method is cleared, and then the asynchronous result processing logic can be entered uniformly without adding any other code logic.

[0047] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, apparatus, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0048] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart illustrations and / or block diagrams. Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.

[0049] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.

[0050] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.

[0051] While specific embodiments of the present invention have been described above, those skilled in the art should understand that the specific embodiments described are merely illustrative and not intended to limit the scope of the present invention. Equivalent modifications and variations made by those skilled in the art in accordance with the spirit of the present invention should be covered within the scope of protection of the claims of the present invention.

Claims

1. A method for dynamically adding asynchronous operations and returning the last added asynchronous result, characterized in that, include: The closure construction process and asynchronous execution process; The closure construction process is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronouss, and defines a public asynchronous method belonging to the closure as a bridge for the state of the last passed-in promise asynchronous. Whenever a promise asynchronous to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous execution is completed as ending the public asynchronous. That is, the state of this promise asynchronous is bridged to the public asynchronous. Simultaneously, a data lock is added to this promise asynchronous and the data lock of the last promise asynchronous in the array is locked. Finally, this promise asynchronous is added to the public array, and both the public asynchronous method and the `add` method are returned. The asynchronous execution process includes: when there are asynchronous promises that need to be executed sequentially, checking whether the variable storing the closure exists; if it exists, calling the add method on the variable storing the closure in sequence for the asynchronous promises that need to be executed; if it does not exist, executing the closure construction process, returning a public asynchronous array and the add method after construction, then calling the add method to add the asynchronous promises to the public array and bridging the state to the public asynchronous array; then clearing the variable storing the closure after waiting for the public asynchronous array to return the result.

2. The method according to claim 1, characterized in that: The add method supports chained calls. Each newly added promise that needs to be executed is added to the public array before the public promise in turn through the add method.

3. The method according to claim 1 or 2, characterized in that: The public asynchronous method is a promise asynchronous method. After the last added promise asynchronous method is executed, the public asynchronous method only returns the result of the last promise asynchronous method added to the method.

4. An apparatus for dynamically adding asynchronous operations and returning the last added asynchronous result, characterized in that, include: Closure building modules and asynchronous execution modules; The closure construction module is used to construct an initialization closure. This closure responds to the `add` method call, adds a promise asynchronous member and remembers all asynchronous members, and has an internal locking state so it will not be destroyed. The closure defines a public array to record all passed-in promise asynchronouss, and defines a public asynchronous function belonging to the closure as a bridge for the state of the last passed-in promise asynchronous. Whenever a promise asynchronous to be executed is passed in, the `add` method defined on the public array defines the subsequent logic after the promise asynchronous execution is completed as ending the public asynchronous function. That is, the state of this promise asynchronous is bridged to the public asynchronous function. At the same time, a data lock is added to this promise asynchronous function, and the data lock of the last promise asynchronous function in the array is locked. Finally, this promise asynchronous function is added to the public array and the public asynchronous function and the `add` method are returned. The asynchronous execution module is used to check whether the variable storing the closure exists when there are asynchronous promises that need to be executed sequentially. If it exists, the add method on the variable storing the closure is called sequentially for each asynchronous promise that needs to be executed. If it does not exist, the closure construction module is executed. After construction, a public asynchronous array and the add method are returned. The asynchronous execution module then calls the add method to add the asynchronous promises to the public array and bridge the state to the public asynchronous array. After waiting for the public asynchronous result to return, clear the variables storing the closure.

5. The apparatus according to claim 4, characterized in that: The add method supports chained calls. Each newly added promise that needs to be executed is added to the public array before the public promise in turn through the add method.

6. The apparatus according to claim 4 or 5, characterized in that: The public asynchronous method is a promise asynchronous method. After the last added promise asynchronous method is executed, the public asynchronous method only returns the result of the last promise asynchronous method added to the method.