A method and device for executing asynchronous processing tasks

By redefining the state of the Promise object and making it change multiple times during asynchronous task processing, the problem that the traditional Promise mechanism cannot handle multiple output results is solved, and flexible processing and efficient response to asynchronous processing tasks are achieved.

CN119645532BActive Publication Date: 2025-09-09BEIJING XIAOMI MOBILE SOFTWARE CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311197864.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-09-15
Publication Date
2025-09-09
Estimated Expiration
2043-09-15

AI Technical Summary

Technical Problem

The traditional Promise mechanism cannot be applied to scenarios where asynchronous processing tasks have multiple output results, because the status information of the Promise object cannot be changed again once it changes to fulfilled or rejected.

Method used

By redefining the state of the Promise object, it can change multiple times during the asynchronous task processing, including the in-place state, exception state, and completion state, allowing the execution results of multiple subtasks to be obtained in sequence.

Benefits of technology

It realizes the effective processing of multiple execution results in asynchronous processing tasks, supports scenarios with multiple output results, and improves the program's responsiveness and concurrent processing capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119645532B_ABST
    Figure CN119645532B_ABST
Patent Text Reader

Abstract

The present disclosure provides a method and device for executing an asynchronous processing task, the method comprising: obtaining a task execution code corresponding to the asynchronous processing task to be executed and a Promise object corresponding to the asynchronous processing task for transmitting the execution result of the task execution code; the task execution code includes execution codes corresponding to multiple subtasks; the state of the Promise object includes an in-place state indicating that the execution of the task execution code corresponding to some of the multiple subtasks has ended; executing the first subtask and obtaining the execution result; in response to the successful execution of the first subtask, calling a resolve method, setting the state of the Promise object to the in-place state, executing the next subtask, obtaining the execution result of the next subtask, and so on, until the asynchronous processing task is completed. According to the present disclosure, asynchronous processing tasks with multiple execution results can be asynchronously processed.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a method, device, electronic device, and machine-readable storage medium for executing asynchronous processing tasks. Background Art

[0002] Asynchronous operations include various processing methods, such as callback functions, Promise mechanisms, and Generator functions. Among them, the traditional Promise mechanism encapsulates asynchronous operations into Promise objects, which represent the final result of the asynchronous operation and have three states: pending, fulfilled, and rejected.

[0003] In current technology, the traditional Promise mechanism prevents a Promise object from changing its state from pending to fulfilled or rejected. This means that a Promise object can only deliver one output result or one exception message. Therefore, this mechanism is unsuitable for asynchronous processing scenarios where multiple output results exist. Summary of the Invention

[0004] The present disclosure provides a method for executing an asynchronous processing task, the method comprising:

[0005] Obtaining a task execution code corresponding to an asynchronous processing task to be executed; and a Promise object corresponding to the asynchronous processing task for delivering an execution result of the task execution code; the Promise object including multiple methods for processing the execution result of the asynchronous processing task; the multiple methods including a resolve method; the asynchronous processing task including multiple subtasks; the task execution code including execution codes corresponding to the multiple subtasks; the state of the Promise object including an in-place state; the in-place state indicating that the execution of the task execution codes corresponding to some of the multiple subtasks has ended;

[0006] Running a task execution code corresponding to a first subtask among the multiple subtasks to execute the first subtask and obtain an execution result of the first subtask;

[0007] In response to the successful execution of the first subtask, calling the resolve method, setting the state of the Promise object to the in-place state, and further running the task execution code corresponding to the next subtask of the first subtask to execute the next subtask and obtain the execution result of the next subtask;

[0008] In response to the successful execution of the next subtask, the resolve method is called to set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the next subtask, and so on, until the task execution code corresponding to the asynchronous processing task to be executed is completed.

[0009] Optionally, the asynchronous processing task includes multiple life cycles; at least some of the multiple subtasks correspond to different life cycles; or, each subtask in the multiple subtasks corresponds to a different life cycle.

[0010] Optionally, the task execution code corresponding to the asynchronous processing task to be executed is JavaScript code.

[0011] Optionally, the method further includes:

[0012] The execution result of the first subtask is passed to a first processing method; wherein the first processing method is used to process the execution result of the first subtask when the state of the Promise object is in place.

[0013] Optionally, the multiple methods include a reject method; the state of the Promise object includes an abnormal state;

[0014] The method further comprises:

[0015] In response to the failure of execution of any subtask among the multiple subtasks, the reject method is called, the state of the Promise object is set to an abnormal state, and the task execution code corresponding to the next subtask of any subtask is further run to execute the next subtask and obtain the execution result of the next subtask.

[0016] Optionally, the method further includes:

[0017] The exception information of any subtask is passed to a second processing method; wherein the second processing method is used to process the exception information of any subtask when the state of the Promise object is an abnormal state.

[0018] Optionally, the multiple methods include a Complete method; the state of the Promise object includes a completed state;

[0019] The method further comprises:

[0020] In response to the completion of execution of all subtasks in the plurality of subtasks, a Complete method is called to set the state of the Promise object to a completed state.

[0021] Optionally, the method further includes:

[0022] The end information indicating that all subtasks have been executed is passed to a third processing method; wherein the third processing method is used to process the end information and output the end information when the state of the Promise object is a completed state.

[0023] Optionally, the asynchronous processing task includes an asynchronous processing task of periodically and asynchronously acquiring memory attribute information of the mobile client electronic device based on a preset time period.

[0024] The present disclosure further provides an apparatus for executing an asynchronous processing task, the apparatus comprising:

[0025] An acquisition module is configured to acquire a task execution code corresponding to an asynchronous processing task to be executed; and a Promise object corresponding to the asynchronous processing task for delivering an execution result of the task execution code; the Promise object includes a plurality of methods for processing the execution result of the asynchronous processing task; the plurality of methods include a resolve method; the asynchronous processing task includes a plurality of subtasks; the task execution code includes execution codes corresponding to the plurality of subtasks; the state of the Promise object includes an in-place state; the in-place state indicates that the execution of the task execution code corresponding to some of the plurality of subtasks has been completed;

[0026] A running module, configured to run a task execution code corresponding to a first subtask among the multiple subtasks to execute the first subtask and obtain an execution result of the first subtask;

[0027] A calling module is used to call the resolve method in response to the successful execution of the first subtask, set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the first subtask to execute the next subtask and obtain the execution result of the next subtask; in response to the successful execution of the next subtask, call the resolve method, set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the next subtask, and so on, until the task execution code corresponding to the asynchronous processing task to be executed is completed.

[0028] The present disclosure further provides an electronic device, comprising a communication interface, a processor, a memory, and a bus, wherein the communication interface, the processor, and the memory are interconnected via the bus;

[0029] The memory stores machine-readable instructions, and the processor executes the execution method of the asynchronous processing task by calling the machine-readable instructions.

[0030] The present disclosure also provides a machine-readable storage medium, wherein the machine-readable storage medium stores machine-readable instructions, and when the machine-readable instructions are called and executed by a processor, the execution method of the asynchronous processing task is implemented.

[0031] The technical solution provided by the present disclosure may at least have the following beneficial effects:

[0032] Through the above embodiments, the task execution code corresponding to the asynchronous processing task to be executed can be obtained; as well as the Promise object corresponding to the asynchronous processing task for delivering the execution result of the task execution code. Since the state of the Promise object can include an in-place state; the in-place state can indicate that the task execution code corresponding to some of the multiple subtasks has been executed, the execution results of each subtask in the multiple subtasks can be obtained in sequence by sequentially running the execution code corresponding to the multiple subtasks contained in the task execution code; if the first subtask in the multiple subtasks is executed successfully, the resolve method can be called to set the state of the Promise object to the in-place state, and further execute subsequent subtasks. In this way, asynchronous processing tasks with multiple execution results can be asynchronously processed. BRIEF DESCRIPTION OF THE DRAWINGS

[0033] Figure 1 The flowchart of an exemplary embodiment shows a method for executing an asynchronous processing task.

[0034] Figure 2 The flowchart of another method for executing an asynchronous processing task is shown in an exemplary embodiment.

[0035] Figure 3 The figure is a hardware structure diagram of an electronic device in which an execution device for asynchronous processing tasks is located, showing an exemplary embodiment.

[0036] Figure 4 The figure is a block diagram of an execution device for asynchronous processing tasks shown in an exemplary embodiment. DETAILED DESCRIPTION

[0037] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all possible embodiments consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.

[0038] It should be noted that in other embodiments, the steps of the corresponding method are not necessarily performed in the order shown and described in this disclosure. In some other embodiments, the method may include more or fewer steps than those described in this disclosure. In addition, a single step described in this disclosure may be broken down into multiple steps for description in other embodiments; and multiple steps described in this disclosure may be combined into a single step for description in other embodiments.

[0039] Asynchronous operations refer to the process where, after an operation is submitted for execution, the program continues to execute subsequent operations without waiting for its result to be returned. When the asynchronous operation is completed, the execution result will be notified through callbacks, events, or other mechanisms.

[0040] Because asynchronous operations allow a program to immediately execute subsequent tasks without waiting for the asynchronous operation to complete, this can improve the program's responsiveness and concurrent processing capabilities. Asynchronous operations are typically used to handle time-consuming operations or interact with external resources.

[0041] Asynchronous operations include various processing methods, such as callback functions, the Promise mechanism, and Generator functions. The Promise mechanism encapsulates asynchronous operations into Promise objects, which represent the final result of the asynchronous operation and have three states: pending, fulfilled, and rejected.

[0042] In actual applications, the Promise object is initially in the pending state. When the asynchronous operation is executed successfully, the state of the Promise object can be changed to the fulfilled state, and the callback function corresponding to the fulfilled state can be called to process the output result of the asynchronous operation; when the asynchronous operation fails, the state of the Promise object can be changed to the rejected state, and the callback function corresponding to the rejected state can be called to handle the exception information.

[0043] In current technology, the traditional Promise mechanism prevents the state of a Promise object from changing to fulfilled or rejected. This means that the Promise object can only deliver a single execution result or exception message. Therefore, this mechanism is unsuitable for asynchronous processing scenarios where multiple execution results exist for a task.

[0044] The following describes the method for executing asynchronous processing tasks provided by the present disclosure through specific embodiments and in combination with specific application scenarios. This method redefines the multiple states of the Promise object so that the state information of the Promise object can change multiple times during the processing of the asynchronous task, thereby enabling the Promise mechanism to be applied to asynchronous processing scenarios where the asynchronous processing task has multiple output results.

[0045] During implementation, a task execution code corresponding to the asynchronous processing task to be executed can be obtained; and a Promise object corresponding to the asynchronous processing task for delivering the execution result of the task execution code can be obtained; the Promise object can include multiple methods for processing the execution result of the asynchronous processing task; the multiple methods can include a resolve method; the asynchronous processing task can include multiple subtasks; the task execution code can include execution codes corresponding to the multiple subtasks; the state of the Promise object can include an in-place state; the in-place state can indicate that the execution of the task execution code corresponding to some of the multiple subtasks has ended;

[0046] Furthermore, the task execution code corresponding to the first subtask among the multiple subtasks may be run to execute the first subtask and obtain the execution result of the first subtask;

[0047] Furthermore, in response to the successful execution of the first subtask, the resolve method can be called to set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the first subtask to execute the next subtask and obtain the execution result of the next subtask; in response to the successful execution of the next subtask, the resolve method can be called to set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the next subtask, and so on, until the task execution code corresponding to the asynchronous processing task to be executed is completed.

[0048] Through the above embodiments, the task execution code corresponding to the asynchronous processing task to be executed can be obtained; as well as the Promise object corresponding to the asynchronous processing task for delivering the execution result of the task execution code. Since the state of the Promise object can include an in-place state; the in-place state can indicate that the task execution code corresponding to some of the multiple subtasks has been executed, the execution results of each subtask in the multiple subtasks can be obtained in sequence by sequentially running the execution code corresponding to the multiple subtasks contained in the task execution code; if the first subtask in the multiple subtasks is executed successfully, the resolve method can be called to set the state of the Promise object to the in-place state, and further execute subsequent subtasks. In this way, asynchronous processing tasks with multiple execution results can be asynchronously processed.

[0049] The present disclosure is described below through specific embodiments and in combination with specific application scenarios.

[0050] See Figure 1 , Figure 1 The flowchart of an exemplary embodiment shows a method for executing an asynchronous processing task.

[0051] The execution subject of the present disclosure may be a JavaScript engine, which is not limited in the present disclosure.

[0052] like Figure 1 As shown, the above JavaScript engine can perform the following steps:

[0053] Step 102, obtain the task execution code corresponding to the asynchronous processing task to be executed; and, a Promise object corresponding to the above-mentioned asynchronous processing task for delivering the execution result of the above-mentioned task execution code; the above-mentioned Promise object may include multiple methods for processing the execution result of the above-mentioned asynchronous processing task; the above-mentioned multiple methods may include a resolve method; the above-mentioned asynchronous processing task may include multiple subtasks; the above-mentioned task execution code may include execution codes corresponding to the above-mentioned multiple subtasks; the state of the above-mentioned Promise object may include an in-place state; the above-mentioned in-place state may indicate that the execution of the task execution code corresponding to some of the above-mentioned multiple subtasks has been completed.

[0054] The present disclosure does not limit the specific content of the asynchronous processing task to be executed. For example, the asynchronous processing task may include an asynchronous processing task of asynchronously obtaining memory attribute information of a mobile client electronic device, or an asynchronous processing task of asynchronously generating multiple random numbers.

[0055] In one embodiment shown, the asynchronous processing task may include an asynchronous processing task of periodically and asynchronously acquiring memory attribute information of the mobile client electronic device based on a preset time period.

[0056] For example, the preset time period may include one minute, and the JavaScript engine may asynchronously obtain the memory attribute information of the mobile client electronic device every minute by executing the asynchronous processing task.

[0057] In one embodiment shown, the task execution code corresponding to the asynchronous processing task to be executed may be JavaScript code.

[0058] For example, the JavaScript engine may obtain and execute JavaScript code corresponding to the asynchronous processing task to be executed.

[0059] The JavaScript engine may also obtain a Promise object corresponding to the asynchronous processing task, and the Promise object may be used to pass the execution result of the task execution code to a subsequent processing method.

[0060] The Promise object may further include multiple methods for processing the execution result of the asynchronous processing task, which is not limited in this disclosure. For example, the multiple methods may include a resolve method, a reject method, and a complete method.

[0061] The above-mentioned asynchronous processing task may include multiple subtasks, and the above-mentioned task execution code may include task execution codes corresponding to the multiple subtasks. By executing the multiple subtasks in sequence, the asynchronous processing task with multiple execution results can be asynchronously processed.

[0062] Among them, the state of the Promise object corresponding to the above-mentioned asynchronous processing task may include an in-place state, which may indicate that the execution of the task execution code corresponding to some of the above-mentioned multiple subtasks has been completed. That is to say, if the state of the Promise object is in-place state, the Promise object can be used to deliver the execution result of the task execution code of the above-mentioned subtask.

[0063] Regarding the specific name of the in-place state, illustratively, the in-place state may be the Started state, which is not limited in the present disclosure.

[0064] In one embodiment shown, the asynchronous processing task may include multiple life cycles; at least some of the multiple subtasks may correspond to different life cycles; or, each of the multiple subtasks may correspond to a different life cycle.

[0065] For example, the asynchronous processing task may be an asynchronous processing task that periodically and asynchronously obtains memory attribute information of a mobile client electronic device based on a preset duration. The asynchronous processing task may include multiple life cycles, wherein the multiple life cycles may include a first life cycle for asynchronously obtaining the memory attribute information of the mobile client electronic device, a second life cycle for asynchronously obtaining the memory attribute information of the mobile client electronic device, a third life cycle for asynchronously obtaining the memory attribute information of the mobile client electronic device, and so on. The multiple subtasks may include subtask_1, subtask_2, and subtask_3, wherein at least some of the multiple subtasks correspond to different life cycles, such as subtask_1 and subtask_2 corresponding to the first life cycle, and subtask_3 corresponding to the second life cycle.

[0066] For another example, the multiple subtasks may include subtask_1 and subtask_2, wherein each subtask in the multiple subtasks corresponds to a different life cycle, and it may be that subtask_1 corresponds to the first life cycle, and subtask_2 corresponds to the second life cycle.

[0067] In this way, the correspondence between the life cycle and the subtask can be set based on actual needs, thereby improving the flexibility of asynchronous processing.

[0068] It should be noted that the above-mentioned Promise object is only an exemplary object name. In the present disclosure, the name of the object may also be FlowPromise.

[0069] Step 104 : Run the task execution code corresponding to the first subtask among the plurality of subtasks to execute the first subtask and obtain the execution result of the first subtask.

[0070] The JavaScript engine may execute the multiple subtasks in sequence according to the execution order of the multiple subtasks. The JavaScript engine may first run the task execution code corresponding to the first subtask among the multiple subtasks to execute the first subtask, and may obtain the execution result of the first subtask.

[0071] Step 106: In response to the successful execution of the above-mentioned first subtask, the resolve method is called to set the state of the above-mentioned Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the above-mentioned first subtask to execute the above-mentioned next subtask and obtain the execution result of the above-mentioned next subtask; in response to the successful execution of the above-mentioned next subtask, the resolve method is called to set the state of the above-mentioned Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the above-mentioned next subtask, and so on, until the above-mentioned task execution code corresponding to the asynchronous processing task to be executed is completed.

[0072] For example, the above-mentioned multiple subtasks may include subtask_1, subtask_2 and subtask_3; the above-mentioned JavaScript engine may call the resolve method in response to the successful execution of subtask_1, set the state of the above-mentioned Promise object to the in-place state, and may further run the task execution code corresponding to the above-mentioned subtask_2 to execute the above-mentioned subtask_2, and obtain the execution result of the subtask_2; the above-mentioned JavaScript engine may call the resolve method in response to the successful execution of the above-mentioned subtask_2, set the state of the above-mentioned Promise object to the in-place state, and may further run the task execution code corresponding to the above-mentioned subtask_3 to execute the above-mentioned subtask_3, and obtain the execution result of the subtask_3; the above-mentioned JavaScript engine may call the resolve method in response to the successful execution of the above-mentioned subtask_3, and set the state of the above-mentioned Promise object to the in-place state.

[0073] In this way, asynchronous processing tasks with multiple execution results can be processed asynchronously in sequence.

[0074] In one embodiment shown, when the JavaScript engine calls the resolve method, it passes the execution result of the first subtask to the first processing method; wherein the first processing method is used to process the execution result of the first subtask when the state of the Promise object is in place.

[0075] For example, since the subtask_1 is executed successfully, the JavaScript engine may pass the execution result of the subtask_1 to the first processing method to process the execution result of the subtask_1.

[0076] For another example, the JavaScript engine may pass the execution result of the subtask_1 to the first processing method to output the execution result of the subtask_1.

[0077] In this way, each of the multiple execution results of the asynchronous processing task can be processed.

[0078] In one embodiment shown, the above-mentioned multiple methods may include a reject method, which can be called when any of the above-mentioned multiple subtasks fails to execute; the above-mentioned JavaScript engine can also call the reject method in response to the failure of any of the above-mentioned multiple subtasks to execute, set the state of the above-mentioned Promise object to an abnormal state, and further run the task execution code corresponding to the next subtask of any of the above-mentioned subtasks to execute the above-mentioned next subtask and obtain the execution result of the above-mentioned next subtask.

[0079] For example, the above-mentioned multiple subtasks may include subtask_1, subtask_2 and subtask_3; the above-mentioned JavaScript engine may call the resolve method in response to the successful execution of subtask_1, set the state of the above-mentioned Promise object to the in-place state, and further run the task execution code corresponding to the above-mentioned subtask_2 to execute the above-mentioned subtask_2, and obtain the execution result of the subtask_2; the above-mentioned JavaScript engine may call the reject method in response to the failure of the execution of the above-mentioned subtask_2, set the state of the Promise object to the abnormal state, and further run the task execution code corresponding to the above-mentioned subtask_3 to execute the above-mentioned subtask_3, and obtain the execution result of the subtask_3.

[0080] In this way, it is convenient to handle the abnormal information of the subtask that failed to execute later.

[0081] Regarding the specific name of the abnormal state, illustratively, the in-place state may be a Reject state, which is not limited in the present disclosure.

[0082] In one embodiment shown, the above-mentioned JavaScript engine can also pass the exception information of any of the above-mentioned subtasks to the second processing method when calling the above-mentioned reject method; wherein the above-mentioned second processing method can be used to process the exception information of any of the above-mentioned subtasks when the state of the above-mentioned Promise object is an abnormal state.

[0083] For example, since the execution of the subtask_2 fails, the JavaScript engine may pass the execution result of the subtask_2 to the second processing method to process the exception information of the subtask_2.

[0084] In this way, any abnormal information in the multiple execution results of the above asynchronous processing task can be processed.

[0085] In one embodiment shown, the above-mentioned multiple methods may include a reject method, which can be called when any of the above-mentioned multiple subtasks fails to execute, and can be used to terminate the above-mentioned asynchronous processing task; the above-mentioned JavaScript engine can also call the reject method in response to the failure of any of the above-mentioned multiple subtasks to execute, and set the state of the above-mentioned Promise object to an abnormal termination state to terminate the asynchronous processing task.

[0086] For example, in response to the failure of the execution of the subtask_2, the JavaScript engine may call a reject method to set the state of the Promise object to an abnormal termination state to terminate the asynchronous processing task.

[0087] In this way, when an exception occurs in the asynchronous processing task, the asynchronous processing task can be terminated in time.

[0088] In one embodiment, the multiple methods may include a Complete method; the Promise object's status may include a Completed state; if all of the multiple subtasks are completed, the Complete method may be called to set the Promise object's status to Completed, thereby terminating the asynchronous processing task. The Complete method may only be called once; when the Promise object's status is Completed, the completion information is processed; the Promise object cannot be used to convey the execution result of the task execution code.

[0089] In response to the completion of execution of all of the multiple subtasks, the JavaScript engine may call a Complete method to set the state of the Promise object to a completed state.

[0090] For example, the multiple subtasks may include subtask_1, subtask_2, and subtask_3; in response to the successful execution of subtask_1, the JavaScript engine may call the resolve method to set the state of the Promise object to the in-place state, and may further run the task execution code corresponding to subtask_2 to execute subtask_2 and obtain the execution result of subtask_2; in response to the successful execution of subtask_2, the JavaScript engine may call the resolve method to set the state of the Promise object to the in-place state, and may further run the task execution code corresponding to subtask_3 to execute subtask_3 and obtain the execution result of subtask_3; in response to the successful execution of subtask_3, the JavaScript engine may call the resolve method to set the state of the Promise object to the in-place state. In response to the completion of the execution of the three subtasks, the JavaScript engine may call the Complete method to set the state of the Promise object to the completed state.

[0091] In this way, the asynchronous processing task can be terminated when the asynchronous processing task is completed.

[0092] Regarding the specific name of the completion state, illustratively, the in-place state may be the Completed state, which is not limited in the present disclosure.

[0093] In one embodiment shown, the above-mentioned JavaScript engine can also pass the end information indicating the completion of execution of all the above-mentioned subtasks to the third processing method when calling the above-mentioned Complete method; wherein the above-mentioned third processing method can be used to process the end information when the state of the above-mentioned Promise object is the completed state.

[0094] The present disclosure does not limit the specific content of processing the end information. For example, the end information can be output, and other operations corresponding to the asynchronous processing task can be performed based on the end information.

[0095] For example, when calling the Complete method, the JavaScript engine may also pass completion information indicating that the three subtasks are completed to the third processing method to output the completion information.

[0096] In this way, when the asynchronous processing task is completed, the end information can be subsequently processed.

[0097] In order to enable those skilled in the art to better understand the technical solutions in the embodiments of the present disclosure, the following takes the above-mentioned multiple subtasks including three subtasks as an example, combined with the following Figure 2 The flowchart of the method for executing an asynchronous processing task is shown to illustrate the embodiment of the present disclosure.

[0098] See Figure 2 , Figure 2 The flowchart of another method for executing an asynchronous processing task is shown in an exemplary embodiment.

[0099] Step 202: Obtain the task execution code corresponding to the asynchronous processing task to be executed; and the Promise object corresponding to the asynchronous processing task and used to deliver the execution result of the task execution code.

[0100] The above-mentioned JavaScript engine can obtain the task execution code corresponding to the asynchronous processing task to be executed; and the Promise object corresponding to the above-mentioned asynchronous processing task for delivering the execution result of the above-mentioned task execution code; the above-mentioned Promise object may include a resolve method, a reject method and a Complete method for processing the execution result of the above-mentioned asynchronous processing task; the above-mentioned asynchronous processing task may include subtask_1, subtask_2 and subtask_3; the above-mentioned task execution code may include task execution codes corresponding to the above-mentioned multiple subtasks; the state of the above-mentioned Promise object may include an in-place state; the in-place state may indicate that the execution of the task execution code corresponding to some of the above-mentioned multiple subtasks has been completed.

[0101] Step 204 : Run the task execution code corresponding to the above subtask_1 to execute the above subtask_1 and obtain the execution result of the subtask_1 .

[0102] The above-mentioned JavaScript engine can run the task execution code corresponding to the above-mentioned subtask_1 to execute the above-mentioned subtask_1, and can obtain the execution result of the subtask_1; the above-mentioned JavaScript engine can call the resolve method in response to the successful execution of the subtask_1, set the state of the above-mentioned Promise object to the in-place state, and pass the execution result of the above-mentioned subtask_1 to the first processing method to process the execution result of the subtask_1.

[0103] Step 206: Run the task execution code corresponding to the above subtask_2 to execute the above subtask_2, and obtain the execution result of the subtask_2.

[0104] The above-mentioned JavaScript engine can further run the task execution code corresponding to the above-mentioned subtask_2 to execute the above-mentioned subtask_2, and can obtain the execution result of the subtask_2; the above-mentioned JavaScript engine can call the reject method in response to the failure of the execution of the above-mentioned subtask_2, set the state of the Promise object to an abnormal state, and pass the execution result of the above-mentioned subtask_2 to the second processing method to process the exception information of the subtask_2.

[0105] Step 208: Run the task execution code corresponding to the above subtask_3 to execute the above subtask_3, and obtain the execution result of the subtask_3.

[0106] The above-mentioned JavaScript engine can further run the task execution code corresponding to the above-mentioned subtask_3 to execute the above-mentioned subtask_3, and can obtain the execution result of the subtask_3; the above-mentioned JavaScript engine can call the resolve method in response to the successful execution of the above-mentioned subtask_3, set the state of the above-mentioned Promise object to the in-place state, and pass the execution result of the above-mentioned subtask_3 to the first processing method to process the execution result of the subtask_3.

[0107] Step 210: In response to the completion of the execution of the three subtasks, the Complete method is called to set the state of the Promise object to the completed state to terminate the asynchronous processing task.

[0108] The above-mentioned JavaScript engine can also call the Complete method in response to the completion of the above-mentioned three subtasks, set the state of the above-mentioned Promise object to the completed state, and pass the end information indicating the completion of the above-mentioned three subtasks to the third processing method to output the end information.

[0109] Corresponding to the above-mentioned embodiment of the method for executing an asynchronous processing task, the present disclosure also provides an embodiment of an apparatus for executing an asynchronous processing task.

[0110] See Figure 3 , Figure 3This is a hardware structure diagram of an electronic device where an execution device for an asynchronous processing task is located, which is shown as an exemplary embodiment. At the hardware level, the device includes a processor 302, an internal bus 304, a network interface 306, a memory 308, and a non-volatile memory 310, and of course may also include hardware required for other services. One or more embodiments of the present disclosure can be implemented based on software, such as the processor 302 reading the corresponding computer program from the non-volatile memory 310 into the memory 308 and then running it. Of course, in addition to software implementation, one or more embodiments of the present disclosure do not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc., that is, the execution subject of the following processing flow is not limited to each logic unit, but can also be hardware or logic devices.

[0111] See Figure 4 , Figure 4 This is a block diagram of an execution device for asynchronous processing tasks shown in an exemplary embodiment. The execution device for asynchronous processing tasks can be applied to Figure 3 The electronic device shown in the figure is used to implement the technical solution of the present disclosure. The device may include:

[0112] Acquisition module 402 is used to obtain the task execution code corresponding to the asynchronous processing task to be executed; and a Promise object corresponding to the asynchronous processing task for delivering the execution result of the task execution code; the Promise object includes multiple methods for processing the execution result of the asynchronous processing task; the multiple methods include a resolve method; the asynchronous processing task includes multiple subtasks; the task execution code includes execution codes corresponding to the multiple subtasks; the state of the Promise object includes an in-place state; the in-place state indicates that the execution of the task execution code corresponding to some of the multiple subtasks has been completed;

[0113] The running module 404 is used to run the task execution code corresponding to the first subtask among the plurality of subtasks to execute the first subtask and obtain the execution result of the first subtask;

[0114] Calling module 406 is used to call the resolve method in response to the successful execution of the above-mentioned first subtask, set the state of the above-mentioned Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the above-mentioned first subtask to execute the above-mentioned next subtask and obtain the execution result of the above-mentioned next subtask; in response to the successful execution of the above-mentioned next subtask, call the resolve method, set the state of the above-mentioned Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the above-mentioned next subtask, and so on, until the above-mentioned task execution code corresponding to the asynchronous processing task to be executed is completed.

[0115] In this embodiment, the asynchronous processing task includes multiple life cycles; at least some of the multiple subtasks may correspond to different life cycles; or, each of the multiple subtasks may correspond to a different life cycle.

[0116] In this embodiment, the task execution code corresponding to the asynchronous processing task to be executed may be JavaScript code.

[0117] In this embodiment, the above device may further include a transmission module, which is used to:

[0118] The execution result of the first subtask is passed to the first processing method; wherein the first processing method is used to process the execution result of the first subtask when the state of the Promise object is in the in-place state.

[0119] In this embodiment, the above-mentioned multiple methods may include a reject method; the state of the above-mentioned Promise object may include an abnormal state;

[0120] The calling module 406 can also be used to:

[0121] In response to the failure of execution of any of the above multiple subtasks, the reject method is called, the state of the above Promise object is set to an abnormal state, and the task execution code corresponding to the next subtask of any of the above subtasks is further run to execute the above next subtask and obtain the execution result of the above next subtask.

[0122] In this embodiment, the above-mentioned transmission module can also be used for:

[0123] The exception information of any of the above subtasks is passed to the second processing method; wherein the second processing method is used to process the exception information of any of the above subtasks when the state of the above Promise object is an abnormal state.

[0124] In this embodiment, the above-mentioned multiple methods may include a Complete method; the state of the above-mentioned Promise object may include a completed state;

[0125] The calling module 406 can also be used to:

[0126] In response to the completion of execution of all of the multiple subtasks, the Complete method is called to set the state of the Promise object to a completed state.

[0127] In this embodiment, the above-mentioned transmission module can also be used for:

[0128] The end information indicating that all the above subtasks have been executed is passed to the third processing method; wherein the third processing method is used to process the end information and output the end information when the state of the above Promise object is the completed state.

[0129] In this embodiment, the asynchronous processing task may include an asynchronous processing task of periodically and asynchronously acquiring memory attribute information of the mobile client electronic device based on a preset time period.

[0130] The implementation process of the functions and effects of each module in the device is specifically described in the implementation process of the corresponding steps in the method, which will not be repeated here.

[0131] For the device embodiments, since they basically correspond to the method embodiments, the relevant parts can be referred to the partial description of the method embodiments. The device embodiments described above are only illustrative, wherein the modules described as separate components may or may not be physically separated, and the components displayed as modules may or may not be physical modules, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules can be selected according to actual needs to achieve the purpose of the disclosed solution. A person of ordinary skill in the art can understand and implement it without making any creative efforts.

[0132] The systems, devices, or modules described in the embodiments may be implemented by computer chips or physical devices, or by products having certain functions. A typical implementation device is a computer, which may be in the form of a personal computer, laptop computer, cellular phone, camera phone, smartphone, personal digital assistant, media player, navigation device, email device, game console, tablet computer, wearable device, or any combination of these devices.

[0133] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0134] Memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. Memory is an example of a computer-readable medium.

[0135] Computer-readable media include permanent and non-permanent, removable and non-removable media that can be used to store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassettes, disk storage, quantum memory, graphene-based storage media or other magnetic storage devices, or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer-readable media does not include transitory media such as modulated data signals and carrier waves.

[0136] It should also be noted that the terms "comprises," "includes," or any other variations thereof are intended to encompass non-exclusive inclusion, such that a process, method, commodity, or apparatus that includes a series of elements includes not only those elements but also other elements not explicitly listed, or includes elements inherent to such process, method, commodity, or apparatus. In the absence of further limitations, an element defined by the phrase "comprises a ..." does not exclude the presence of other identical elements in the process, method, commodity, or apparatus that includes the element.

[0137] The foregoing description describes specific embodiments of the present disclosure. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims can be performed in an order different from that described in the embodiments and still achieve the desired results. In addition, the processes depicted in the accompanying drawings do not necessarily require the specific order shown or the sequential order to achieve the desired results. In certain embodiments, multitasking and parallel processing are also possible or may be advantageous.

[0138] The terms used in one or more embodiments of the present disclosure are for the purpose of describing specific embodiments only and are not intended to limit one or more embodiments of the present disclosure. The singular forms "a," "the," and "the" used in one or more embodiments of the present disclosure and the appended claims are also intended to include plural forms unless the context clearly indicates otherwise. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.

[0139] It should be understood that although the terms first, second, third, etc. may be used to describe various information in one or more embodiments of the present disclosure, such information should not be limited to these terms. These terms are only used to distinguish information of the same type from each other. For example, without departing from the scope of one or more embodiments of the present disclosure, the first information may also be referred to as the second information, and similarly, the second information may also be referred to as the first information. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".

[0140] The above description is merely a preferred embodiment of one or more embodiments of the present disclosure and is not intended to limit one or more embodiments of the present disclosure. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of one or more embodiments of the present disclosure shall be included in the scope of protection of one or more embodiments of the present disclosure.

[0141] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

Claims

1. A method for executing an asynchronous processing task, the method comprising: Get the task execution code corresponding to the asynchronous processing task to be executed; And, a Promise object corresponding to the asynchronous processing task and used to deliver the execution result of the task execution code; The Promise object includes multiple methods for processing the execution results of the asynchronous processing task; the multiple methods include a resolve method; the asynchronous processing task includes multiple subtasks; the task execution code includes task execution codes corresponding to the multiple subtasks; the state of the Promise object includes an in-place state; the in-place state indicates that the execution of the task execution codes corresponding to some of the multiple subtasks has ended; Running a task execution code corresponding to a first subtask among the multiple subtasks to execute the first subtask and obtain an execution result of the first subtask; In response to the successful execution of the first subtask, calling the resolve method, setting the state of the Promise object to the in-place state, and further running the task execution code corresponding to the next subtask of the first subtask to execute the next subtask and obtain the execution result of the next subtask; In response to the successful execution of the next subtask, the resolve method is called to set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the next subtask, and so on, until the task execution code corresponding to the asynchronous processing task to be executed is completed.

2. According to the method according to claim 1, the asynchronous processing task includes multiple life cycles; at least some of the multiple subtasks correspond to different life cycles; or, each subtask in the multiple subtasks corresponds to a different life cycle. 3 . The method according to claim 1 , wherein the task execution code corresponding to the asynchronous processing task to be executed is JavaScript code.

4. The method according to claim 1, further comprising: The execution result of the first subtask is passed to a first processing method; wherein the first processing method is used to process the execution result of the first subtask when the state of the Promise object is in place.

5. The method according to claim 1, wherein the plurality of methods include a reject method; and the state of the Promise object includes an exception state; The method further comprises: In response to the failure of execution of any subtask among the multiple subtasks, the reject method is called, the state of the Promise object is set to an abnormal state, and the task execution code corresponding to the next subtask of any subtask is further run to execute the next subtask and obtain the execution result of the next subtask.

6. The method according to claim 5, further comprising: The exception information of any subtask is passed to a second processing method; wherein the second processing method is used to process the exception information of any subtask when the state of the Promise object is an abnormal state.

7. The method of claim 1, wherein the plurality of methods comprises a Complete method; and the state of the Promise object comprises a Complete state; The method further comprises: In response to the completion of execution of all subtasks in the plurality of subtasks, a Complete method is called to set the state of the Promise object to a completed state.

8. The method according to claim 7, further comprising: The end information indicating that all subtasks have been executed is passed to a third processing method; wherein the third processing method is used to process the end information and output the end information when the state of the Promise object is a completed state.

9. The method according to claim 1, wherein the asynchronous processing task comprises an asynchronous processing task of periodically and asynchronously obtaining memory attribute information of the mobile client electronic device based on a preset time period.

10. A device for executing asynchronous processing tasks, the device comprising: An acquisition module is used to obtain the task execution code corresponding to the asynchronous processing task to be executed; And, a Promise object corresponding to the asynchronous processing task and used to deliver the execution result of the task execution code; The Promise object includes multiple methods for processing the execution results of the asynchronous processing task; the multiple methods include a resolve method; the asynchronous processing task includes multiple subtasks; the task execution code includes execution codes corresponding to the multiple subtasks; the state of the Promise object includes an in-place state; the in-place state indicates that the execution of the task execution code corresponding to some of the multiple subtasks has ended; A running module, configured to run a task execution code corresponding to a first subtask among the multiple subtasks to execute the first subtask and obtain an execution result of the first subtask; A calling module is used to call the resolve method in response to the successful execution of the first subtask, set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the first subtask to execute the next subtask and obtain the execution result of the next subtask; in response to the successful execution of the next subtask, call the resolve method, set the state of the Promise object to the in-place state, and further run the task execution code corresponding to the next subtask of the next subtask, and so on, until the task execution code corresponding to the asynchronous processing task to be executed is completed.

11. A user equipment, comprising: processor; a memory for storing processor-executable instructions; The processor is configured to implement the method according to any one of claims 1 to 9.

12. A machine-readable storage medium, wherein the machine-readable storage medium stores machine-readable instructions, wherein when the machine-readable instructions are called and executed by a processor, the method according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Promise-based chained asynchronous request processing method and apparatus and electronic device

    CN109669720A

  • Call stacks for asynchronous programs

    WO2014126826A1