Method and device for executing asynchronous processing task
By introducing multiple states and methods into the Promise mechanism, the problem of asynchronous processing tasks in which the traditional Promise mechanism cannot handle multiple output results is solved, and asynchronous processing and exception handling of the execution results of multiple subtasks is realized.
Patent Information
- Application Number
- CN202311197864.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-15
- Publication Date
- 2025-03-18
- Estimated Expiration
- 2043-09-15
AI Technical Summary
The traditional Promise mechanism cannot be applied to scenarios where multiple output results exist for asynchronous processing tasks, because the state of the Promise object can only change from pending to fulfilled or rejected, and cannot be changed again.
Provides an execution method of asynchronous processing tasks. By obtaining task execution code and Promise objects corresponding to asynchronous processing tasks, it uses multiple methods of the Promise object (such as resolve, reject, Complete) and state (such as in-place state) to process the execution results of multiple subtasks.
Asynchronous processing of asynchronous processing tasks with multiple execution results is realized, the execution results of multiple subtasks can be obtained in turn, and exception information or terminated tasks if necessary.
Smart Images

Figure CN119645532A_ABST
Abstract
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 multiple processing methods, such as callback functions, Promise mechanisms, and Generator functions, etc. Among them, the traditional Promise mechanism encapsulates asynchronous operations into Promise objects, which represent the final results of asynchronous operations. The object includes three states: pending, fulfilled, and rejected.
[0003] In the current technology, in the traditional Promise mechanism, after the state information of the Promise object changes from pending to fulfilled or rejected, it cannot be changed again, that is, the Promise object can only transmit one output result or one exception information. Therefore, this mechanism cannot be applied to asynchronous processing scenarios where there are multiple output results of asynchronous processing tasks. Summary of the invention
[0004] The present disclosure provides a method for executing an asynchronous processing task, the method comprising:
[0005] Obtain 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 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 of 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 of the multiple subtasks, the reject method is called to set the state of the Promise object to an abnormal state, and further run the task execution code corresponding to the next subtask of any subtask 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 completion state;
[0019] The method further comprises:
[0020] In response to the completion of execution of all subtasks in the multiple 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 also provides an execution device for asynchronous processing tasks, the device comprising:
[0025] An acquisition module, used for acquiring a task execution code corresponding to an asynchronous processing task to be executed; and a Promise object corresponding to the asynchronous processing task and used 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, used for 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;
[0027] A calling module, which 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 also 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 include 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; and the Promise object corresponding to the asynchronous processing task for transmitting the execution result of the task execution code can be obtained. 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 of the multiple subtasks can be obtained in sequence by running the execution code corresponding to the multiple subtasks contained in the task execution code in sequence; if the first subtask of 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 the subsequent subtasks. In this way, asynchronous processing tasks with multiple execution results can be processed asynchronously. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] Figure 1 The present invention is a flowchart of a method for executing an asynchronous processing task, which is shown in an exemplary embodiment.
[0034] Figure 2 It is a flowchart showing another method for executing an asynchronous processing task according to an exemplary embodiment.
[0035] Figure 3 It is a hardware structure diagram of an electronic device where an execution device for asynchronous processing tasks is located, showing an exemplary embodiment.
[0036] Figure 4 It 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, examples of which are shown in the accompanying drawings. When the following description refers to the drawings, the same numbers in different drawings represent the same or similar elements unless otherwise indicated. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some 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 steps included in the method may be more or less than those described in this disclosure. In addition, a single step described in this disclosure may be decomposed into multiple steps for description in other embodiments; and multiple steps described in this disclosure may also be combined into a single step for description in other embodiments.
[0039] Asynchronous operation means that during the execution of a program, when an operation is submitted for execution, it does not immediately wait for its result to be returned, but continues to execute subsequent operations. When the asynchronous operation is completed, the execution result will be notified through callbacks, events or other mechanisms.
[0040] Since asynchronous operations allow the program to immediately execute subsequent tasks of the asynchronous operation without waiting for the asynchronous operation to complete, the program's responsiveness and concurrent processing capabilities can be improved. Asynchronous operations are usually applied to scenarios where time-consuming operations are processed or interactions with external resources are performed.
[0041] Asynchronous operations include multiple processing methods, such as callback functions, Promise mechanisms, and Generator functions, etc. Among them, the Promise mechanism encapsulates asynchronous operations into Promise objects, which represent the final results of asynchronous operations. The object includes 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 process the exception information.
[0043] In the current technology, in the traditional Promise mechanism, after the state information of the Promise object changes to the fulfilled state or the rejected state, it cannot be changed again, that is, the Promise object can only transmit one execution result or one exception information. Therefore, this mechanism cannot be applied to asynchronous processing scenarios where asynchronous processing tasks have multiple execution results.
[0044] The following is an introduction to the execution method of asynchronous processing tasks provided by the present disclosure through specific embodiments and in combination with specific application scenarios. The method redefines multiple states of the Promise object so that the state information of the Promise object can be changed 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; and the Promise object corresponding to the asynchronous processing task for transmitting the execution result of the task execution code can be obtained. 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 of the multiple subtasks can be obtained in sequence by running the execution code corresponding to the multiple subtasks contained in the task execution code in sequence; if the first subtask of 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 the subsequent subtasks. In this way, asynchronous processing tasks with multiple execution results can be processed asynchronously.
[0049] The present disclosure is described below through specific embodiments and in combination with specific application scenarios.
[0050] See also Figure 1 , Figure 1 The present invention is a flowchart of a method for executing an asynchronous processing task, which is shown in an exemplary embodiment.
[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 asynchronous processing task for delivering the execution result of the above task execution code; the above Promise object may include multiple methods for processing the execution result of the above asynchronous processing task; the above multiple methods may include a resolve method; the above asynchronous processing task may include multiple subtasks; the above task execution code may include execution codes corresponding to the above multiple subtasks; the state of the above Promise object may include an in-place state; the above in-place state may indicate that the execution of the task execution code corresponding to some of the above 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 acquiring 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 for 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 a 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 also include multiple methods for processing the execution result of the asynchronous processing task, which is not limited in the present 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 processed asynchronously.
[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 pass 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 for periodically and asynchronously acquiring memory attribute information of a mobile client electronic device based on a preset time period, and the asynchronous processing task may include multiple life cycles, wherein the multiple life cycles may include a first life cycle for asynchronously acquiring memory attribute information of a mobile client electronic device, a second life cycle for asynchronously acquiring memory attribute information of a mobile client electronic device, a third life cycle for asynchronously acquiring memory attribute information of a mobile client electronic device, etc. 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, respectively, and may refer to 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 of the multiple subtasks corresponds to a different life cycle, and it may mean that subtask_1 corresponds to the first life cycle, and subtask_2 corresponds to the second life cycle.
[0067] In this way, the corresponding relationship 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 Promise object is only an exemplary object name. In the present disclosure, the name of the object may also be FlowPromise.
[0069] Step 104 , running 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, 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 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.
[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 asynchronously processed in sequence.
[0074] In one embodiment shown, when calling the resolve method, the JavaScript engine 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 above subtask_1 is executed successfully, the above JavaScript engine can pass the execution result of the above 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 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 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 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.
[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 JavaScript engine may also pass the exception information of any of the subtasks to a second processing method when calling the reject method; wherein the second processing method may be used to process the exception information of any of the subtasks when the state of the 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 and 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 shown, the above-mentioned multiple methods may include a Complete method; the state of the above-mentioned Promise object may include a completed state; if all of the above-mentioned multiple subtasks are completed, the state of the Promise object may be set to a completed state by calling the Complete method to terminate the asynchronous processing task. The Complete method may only be called once, and when the state of the above-mentioned Promise object is a completed state, the end information is processed. The Promise object cannot be used to transmit the execution result of the task execution code.
[0089] The JavaScript engine may call a Complete method in response to the completion of execution of all of the multiple subtasks, and set the state of the Promise object to a completed state.
[0090] 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 may 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 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 may 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 subtask_3, and set the state of the above-mentioned Promise object to the in-place state. The above-mentioned JavaScript engine may call the Complete method in response to the completion of the execution of the above-mentioned three subtasks, and set the state of the above-mentioned 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 JavaScript engine can also pass the completion information indicating the completion of all the subtasks to a third processing method when calling the Complete method; wherein the third processing method can be used to process the completion information when the state of the 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 may be output, and other operations corresponding to the asynchronous processing task may 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 execution method of the asynchronous processing task is shown to illustrate the embodiment of the present disclosure.
[0098] See also Figure 2 , Figure 2 It is a flowchart of another method for executing an asynchronous processing task according to an exemplary embodiment.
[0099] Step 202, obtaining a 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 transmit 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 transmitting 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, running the task execution code corresponding to the above subtask_1 to execute the above subtask_1, and obtaining 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 can 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, running the task execution code corresponding to the above subtask_2 to execute the above subtask_2, and obtaining 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, running the task execution code corresponding to the above subtask_3 to execute the above subtask_3, and obtaining 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 can 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 above three subtasks, the Complete method is called to set the state of the above Promise object to the completed state to terminate the asynchronous processing task.
[0108] The JavaScript engine may also call the Complete method in response to the completion of the three subtasks, set the state of the Promise object to the completed state, and pass the completion information indicating the completion of the three subtasks to the third processing method to output the completion 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 also Figure 3 , Figure 3It is a hardware structure diagram of an electronic device where an execution device for asynchronous processing tasks 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 the software implementation method, 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 to say, 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 also Figure 4 , Figure 4 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 In the electronic device shown in the figure, the technical solution of the present disclosure is implemented. The device may include:
[0112] An acquisition module 402 is used 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 and used to transmit 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 ended;
[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 a 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 place.
[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 to set the state of the above Promise object to an abnormal state, and further run the task execution code corresponding to the next subtask of any of the above subtasks 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 completion state;
[0125] The calling module 406 can also be used to:
[0126] In response to the completion of execution of all the subtasks in the above multiple subtasks, the Complete method is called to set the state of the above 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 above-mentioned 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 refer to the partial description of the method embodiments. The device embodiments described above are only schematic, 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 may be selected according to actual needs to achieve the purpose of the disclosed solution. A person of ordinary skill in the art may understand and implement it without creative work.
[0132] The system, device or module described in the embodiments may be implemented by a computer chip or entity, or by a product having a certain function. A typical implementation device is a computer, which may be in the form of a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email transceiver, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0133] In a typical configuration, a computer includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0134] The 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. The 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 by 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 temporary computer-readable media (transitory media), such as modulated data signals and carrier waves.
[0136] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0137] The above describes specific embodiments of the present disclosure. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some 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 only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of the present disclosure. The singular forms of "a", "said" 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 other meanings. 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, these information should not be limited to these terms. These terms are only used to distinguish the same type of information 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 determination".
[0140] The above description is only 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: 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 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 task 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 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 described in claim 1, the asynchronous processing task includes multiple life cycles; at least some of the multiple subtasks correspond to different life cycles; or, each of the multiple subtasks corresponds to a different life cycle. 3 . According to the method of claim 1 , 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 abnormal state; The method further comprises: In response to the failure of execution of any of the multiple subtasks, the reject method is called to set the state of the Promise object to an abnormal state, and further run the task execution code corresponding to the next subtask of any subtask 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 according to claim 1, wherein the plurality of methods include a Complete method; the state of the Promise object includes a Complete state; The method further comprises: In response to the completion of execution of all subtasks in the multiple 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. According to the method of claim 1, 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.
10. An execution device for asynchronous processing tasks, the device comprising: An acquisition module, used to acquire a 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 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 ended; A running module, used for 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; A calling module, which 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 used to implement the method according to any one of claims 1 to 9.
12. A machine-readable storage medium storing machine-readable instructions, wherein the machine-readable instructions, when called and executed by a processor, implement the method according to any one of claims 1 to 9.
Citation Information
Patent Citations
Programming method of promise control asynchronous function sequence based on AI and children education
CN108874523A
Promise-based chained asynchronous request processing method and apparatus and electronic device
CN109669720A
Asynchronous interaction method and device, electronic equipment and storage medium
CN113448463A
Method and device for realizing asynchronous control of program
CN116400984A
System and method for task scheduling, signal analysis and remote sensor
EP1724684A1