Function calling method and device, storage medium and electronic device

CN120448153APending Publication Date: 2025-08-08QINGDAO HAIER TECH +2
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510478851.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-04-16
Publication Date
2025-08-08

AI Technical Summary

Technical Problem

[0003]目前函数式编程中,因业务或需求变更,被多次多地方调用的函数变更为异步时,所有相关调用该函数的其他函数都要变更为异步,造成工作量大,重复性机械性工作多且易出错的问题

Benefits of technology

[0015]根据本申请实施例的又一方面,还提供了一种电子装置,包括存储器、处理器及存储在存储器上并可在处理器上运行的计算机程序,其中,上述处理器通过计算机程序执行上述的函数调用方法。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120448153A_ABST
    Figure CN120448153A_ABST
Patent Text Reader

Abstract

The invention discloses a function calling method and device, a storage medium and an electronic device, and relates to the technical field of smart homes.The function calling method includes the steps that under the condition that a first function needing to execute an asynchronous request exists in a main function calling chain, the first function is indicated to generate a pseudo exception mark corresponding to the first function, the pseudo exception mark is used for indicating that the first function is executing and / or waiting to execute the asynchronous request; under the condition that the pseudo exception mark is captured, calling of the first function is ended, calling of a second function in the main function calling chain is executed, and the second function is a function which does not depend on an asynchronous result obtained by executing the asynchronous request by the first function; under the condition that it is determined that the first function completes execution of the asynchronous request and an asynchronous result is generated, calling of a third function in the main function calling chain is executed according to the asynchronous result, and the third function is a function depending on the asynchronous result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of smart home technology, and more specifically, to a function calling method and device, a storage medium, and an electronic device. Background Art

[0002] In front-end development, functional programming is often used to write business logic, and most business logic requires asynchronous programming. When asynchronous async / await (a method of handling asynchronous operations) is used in a certain business logic, all places where the function is called must be converted to asynchronous async / await. When there are many other functions calling the function, this change will become a disaster.

[0003] In current functional programming, when a function that is called multiple times and in multiple places is changed to asynchronous due to changes in business or requirements, all other related functions that call this function must also be changed to asynchronous, resulting in a large workload, a lot of repetitive and mechanical work, and prone to errors.

[0004] Regarding functional programming in related technologies, when one function is changed to an asynchronous function, other functions that call the asynchronous function also need to be changed to asynchronous functions, resulting in a large workload for functional programming. No effective solution has yet been proposed. Summary of the Invention

[0005] The embodiments of the present application provide a function calling method and device, a storage medium, and an electronic device to at least solve the problem of heavy workload of functional programming in the related art, in which, when one function is changed to an asynchronous function, other functions that call the asynchronous function also need to be changed to asynchronous functions.

[0006] According to one embodiment of the embodiments of the present application, a function calling method is provided, comprising: in a case where there is a first function in a main function call chain that needs to execute an asynchronous request, instructing the first function to generate a pseudo-exception mark corresponding to the first function, wherein the pseudo-exception mark is used to indicate that the first function is executing and / or is to execute the asynchronous request; in a case where the pseudo-exception mark is captured, ending the call to the first function, and executing a call to a second function in the main function call chain, wherein the second function is a function that is not dependent on an asynchronous result obtained by executing the asynchronous request by the first function; in a case where it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, executing a call to a third function in the main function call chain according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0007] In an exemplary embodiment, after ending the call to the first function, the method further includes: creating an asynchronous event listener corresponding to the first function, and monitoring the execution status of the first function for the asynchronous request according to the asynchronous event listener; when it is determined through the asynchronous event listener that the execution status of the first function for the asynchronous request is completed, storing the asynchronous result in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain.

[0008] In an exemplary embodiment, after instructing the first function to generate a pseudo-exception marker corresponding to the first function, the method further includes: setting a global exception handling function in the main function call chain, wherein the global exception handling function is used to capture the pseudo-exception marker corresponding to the first function; in a case where the global exception handling function captures the pseudo-exception marker, verifying the function identifier and request type information in the pseudo-exception marker, wherein the request type information is used to indicate the request parameters of the asynchronous request; in a case where it is determined that the function identifier is a function identifier corresponding to a function included in a preset function list, determining that the function identifier verification is passed, and, in a case where it is determined that the request type information is complete, determining that the request type information verification is passed, wherein the function list includes: all functions included in the main function call chain.

[0009] In an exemplary embodiment, before executing a call to the second function in the main function call chain, the method further includes: obtaining a function call data stream between multiple functions in the main function call chain, and determining the dependency relationship of the function call of each function in the main function call chain based on the function call data stream; when it is determined according to the dependency relationship that the fourth function in the main function call chain does not depend on the asynchronous result, but depends on the synchronous result corresponding to the first function, determining whether the first function has completed the execution of the synchronous request, wherein the synchronous result is the result corresponding to the synchronous request; when it is determined that the first function has completed the execution of the synchronous request, determining the fourth function as the second function; and when it is determined according to the dependency relationship that the fifth function in the main function call chain does not depend on the asynchronous result, and does not depend on the synchronous result, determining the fifth function as the second function.

[0010] In an exemplary embodiment, before executing a call to the third function in the main function call chain based on the asynchronous result, the method further includes: determining whether a call result corresponding to the sixth function in the main function call chain exists in a cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; if it is determined that the call result corresponding to the sixth function does not exist in the cache, determining the sixth function as the third function.

[0011] In an exemplary embodiment, the method further includes: when there are multiple first functions in the main function call chain that need to execute asynchronous requests, adding a global increment counter in the main function call chain; when any first function executes an asynchronous request, determining the current value of the global increment counter as the first identifier corresponding to any first function; when the first function after any first function executes the asynchronous request, updating the numerical value corresponding to the global increment counter to the sum of the current value and the target numerical value, and determining the sum as the second identifier corresponding to the first function after any first function; associating each identifier corresponding to each first function with each asynchronous result corresponding to each first function; when executing a call to the third function, determining the target identifier of the first function on which the third function depends, and obtaining the target asynchronous result associated with the target identifier, so as to execute the call to the third function according to the target asynchronous result.

[0012] In an exemplary embodiment, instructing the first function to generate a pseudo-exception marker corresponding to the first function includes: determining whether an asynchronous result corresponding to the first function exists in a cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; if it is determined that the asynchronous result exists in the cache, prohibiting the first function from executing the asynchronous request, and executing the call of each function in the main function call chain in sequence; if it is determined that the asynchronous request does not exist in the cache, instructing the first function to generate the pseudo-exception marker.

[0013] According to another embodiment of the embodiments of the present application, a function calling device is also provided, including: an indication module, which is used to indicate a first function that needs to execute an asynchronous request in a main function call chain to generate a pseudo-exception mark corresponding to the first function, wherein the pseudo-exception mark is used to indicate that the first function is executing and / or is to execute the asynchronous request; a first calling module, which is used to end the call to the first function and execute a call to the second function in the main function call chain when the pseudo-exception mark is captured, wherein the second function is a function that does not depend on the asynchronous result obtained by the first function executing the asynchronous request; a second calling module, which is used to execute a call to a third function in the main function call chain according to the asynchronous result when it is determined that the first function has completed the execution of the asynchronous request and generated the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0014] According to another aspect of the embodiments of the present application, a computer-readable storage medium is provided, in which a computer program is stored. The computer program is configured to execute the above-mentioned function calling method when running.

[0015] According to another aspect of an embodiment of the present application, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the function calling method through the computer program.

[0016] According to another aspect of the embodiments of the present application, a computer program product is provided, including a computer program, wherein the computer program is executed by a processor to perform the method.

[0017] In an embodiment of the present application, when there is a first function in the main function call chain that needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark for indicating that the first function is executing and / or waiting to execute an asynchronous request; when the pseudo-exception mark is captured, the call to the first function is terminated, and the second function in the main function call chain that does not depend on the asynchronous result obtained by executing the asynchronous request of the first function is executed; when it is determined that the first function has completed the execution of the asynchronous request and generated an asynchronous result, the call to the third function in the main function call chain that depends on the asynchronous result is executed according to the asynchronous result. That is to say, in an embodiment of the present application, when the first function needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark for identifying that the first function is executing and / or waiting for an asynchronous operation; when a subsequent function (such as the second function and the third function) captures the pseudo-exception mark, the call to the first function is terminated, and then the second function that does not depend on the asynchronous result corresponding to the first function is executed. After the first function completes the asynchronous request and generates the asynchronous result, the third function can be called. Through the embodiments of the present application, after the pseudo-exception marker is captured, the call to the first function will be ended without being blocked by the asynchronous operation of the first function, thereby solving the problem of heavy workload of functional programming in the related art, in which when one function is changed to an asynchronous function, other functions that call the asynchronous function also need to be changed to asynchronous functions. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0019] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0020] Figure 1 This is a schematic diagram of the hardware environment of a function calling method according to an embodiment of the present application;

[0021] Figure 2 is a flowchart of a function calling method according to an embodiment of the present application;

[0022] Figure 3 is a flowchart of a solution for eliminating the contagion of asynchronous functions in functional programming according to an optional embodiment of the present application;

[0023] Figure 4 This is a structural block diagram of a function calling device according to an embodiment of the present application. DETAILED DESCRIPTION

[0024] In order to enable those skilled in the art to better understand the present invention, the following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments in the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of this application.

[0025] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in a sequence other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0026] According to one aspect of the embodiment of the present application, a function calling method is provided. The function calling method is widely used in smart home (Smart Home), smart home, smart home device ecology, smart residential (IntelligenceHouse) ecology and other whole-house intelligent digital control application scenarios. Optionally, Figure 1 This is a hardware environment diagram of a function calling method according to an embodiment of the present application. In this embodiment, the above function calling method can be applied to Figure 1 In the hardware environment shown in FIG. 1 , a central control device 102 and a server 104 are formed. Figure 1 As shown, the server 104 is connected to the central control device 102 via a network, and can be used to provide services (such as application services, etc.) for the terminal or the client installed on the terminal. A database can be set up on the server or independently of the server to provide data storage services for the server 104. Cloud computing and / or edge computing services can be configured on the server or independently of the server to provide data calculation services for the server 104.

[0027] The aforementioned network may include, but is not limited to, at least one of the following: a wired network and a wireless network. The aforementioned wired network may include, but is not limited to, at least one of the following: a wide area network, a metropolitan area network, and a local area network. The aforementioned wireless network may include, but is not limited to, at least one of the following: WIFI (Wireless Fidelity) and Bluetooth. The central control device 102 may be, but is not limited to, a PC, a mobile phone, a tablet computer, a smart air conditioner, a smart range hood, a smart refrigerator, a smart oven, a smart stove, a smart washing machine, a smart water heater, a smart washing machine, a smart dishwasher, a smart projection device, a smart TV, a smart clothes drying rack, smart curtains, smart audio and video, a smart socket, a smart speaker, a smart fresh air device, smart kitchen and bathroom equipment, smart bathroom equipment, a smart sweeping robot, a smart window cleaning robot, a smart mopping robot, a smart air purification device, a smart steamer, a smart microwave oven, a smart kitchen treasure, a smart purifier, a smart water dispenser, a smart door lock, etc.

[0028] In this embodiment, a function calling method is provided. Figure 2 : is a flowchart of a function calling method according to an embodiment of the present application, which includes the following steps:

[0029] Step S202: If there is a first function in the main function call chain that needs to execute an asynchronous request, instruct the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request;

[0030] Among them, in functional programming, functions are often organized in the form of chain calls, that is, one function calls another function, and so on, forming a series of function calls, namely the main function call chain.

[0031] The above pseudo-exception marker is not an exception or error in the traditional sense, but a specially constructed Promise (error) object or similar data structure, which only marks the information containing asynchronous operations but does not indicate a real error or failure.

[0032] Step S204: When the pseudo exception flag is captured, terminate the call to the first function and execute a call to a second function in the main function call chain, wherein the second function is a function that is independent of the asynchronous result obtained by executing the asynchronous request by the first function;

[0033] Step S206, when it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, execute a call to the third function in the main function call chain according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0034] Through the above steps, when there is a first function in the main function call chain that needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark for indicating that the first function is executing and / or waiting to execute an asynchronous request; when the pseudo-exception mark is captured, the call to the first function is terminated, and the second function in the main function call chain that does not depend on the asynchronous result obtained by executing the asynchronous request of the first function is executed; when it is determined that the first function has completed the execution of the asynchronous request and generated an asynchronous result, the call to the third function in the main function call chain that depends on the asynchronous result is executed according to the asynchronous result. That is to say, in an embodiment of the present application, when the first function needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark to identify that the first function is executing and / or waiting for an asynchronous operation; when a subsequent function (such as the second function and the third function) captures the pseudo-exception mark, the call to the first function is terminated, and then the second function that does not depend on the asynchronous result corresponding to the first function is executed. After the first function completes the asynchronous request and generates the asynchronous result, the third function can be called. Through the embodiments of the present application, after the pseudo-exception marker is captured, the call to the first function will be ended without being blocked by the asynchronous operation of the first function, thereby solving the problem of heavy workload of functional programming in the related art, in which when one function is changed to an asynchronous function, other functions that call the asynchronous function also need to be changed to asynchronous functions.

[0035] Optionally, after the call to the first function ends in the above step S204, the method further includes: creating an asynchronous event listener corresponding to the first function, and monitoring the execution status of the first function for the asynchronous request according to the asynchronous event listener; when it is determined through the asynchronous event listener that the execution status of the first function for the asynchronous request is completed, storing the asynchronous result in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain.

[0036] It is understandable that after the call to the first function is completed, a part of the result can be stored in the cache corresponding to the main function call chain. Specifically: create an asynchronous event listener corresponding to the first function: the asynchronous event listener refers to a mechanism or code snippet specially established to monitor the asynchronous request status of the first function. When the first function is called and starts to execute the asynchronous request, an asynchronous event listener is created synchronously to track the execution status of this asynchronous operation. The asynchronous event listener can be based on the .then() (success callback method) and .catch() (failure callback method) methods of the Promise object, or a custom event listening model, and then the asynchronous event listener can capture the various state changes of the asynchronous request from start to completion in real time.

[0037] The .then() method calls the function registered in the promise when the promise's state changes from "pending" to "fulfilled." It accepts two parameters: a callback function that handles the success value and an optional callback function that handles the failure reason.

[0038] The .catch() method is: when the state of the Promise object becomes "rejected" (rejected, that is, failed), the function registered in the .catch() method will be called. The .catch() method only receives one parameter, a callback function, which is used to handle the reason for failure or error information.

[0039] According to the asynchronous event listener, the execution status of the first function for the asynchronous request is monitored: once the asynchronous event listener is created, it starts to monitor and record the execution status of the first function asynchronous request in real time, including but not limited to whether the request has started, whether there are any errors during the request process, whether the request has been successfully completed, etc.

[0040] When the asynchronous event listener determines that the execution status of the first function's asynchronous request is complete, the asynchronous result is stored in the cache associated with the main function call chain. When the asynchronous event listener confirms that the first function's asynchronous request has successfully completed and obtained the asynchronous result, the asynchronous result is immediately stored in the cache associated with the main function call chain. The cache can be any suitable storage mechanism, such as a memory object, local storage, or database. The cache provides fast access and can share data between different function calls.

[0041] The caching mechanism allows asynchronous results to be saved after the first computation, allowing subsequent calls to the same function to retrieve the result directly without having to execute the time-consuming asynchronous request again. This not only improves program responsiveness but also reduces the frequency of network requests or database queries, thus saving system resources.

[0042] It is understandable that the cache is not limited to storing the asynchronous result of the first function, but as a global or local storage space, it can save the execution results of any function in the main function call chain.

[0043] Optionally, after instructing the first function in the above step S202 to generate a pseudo-exception marker corresponding to the first function, the method further includes: setting a global exception handling function in the main function call chain, wherein the global exception handling function is used to capture the pseudo-exception marker corresponding to the first function; in a case where the global exception handling function captures the pseudo-exception marker, verifying the function identifier and request type information in the pseudo-exception marker, wherein the request type information is used to indicate the request parameters of the asynchronous request; in a case where it is determined that the function identifier is a function identifier corresponding to a function included in a preset function list, determining that the function identifier verification is passed; and, in a case where it is determined that the request type information is complete, determining that the request type information verification is passed, wherein the function list includes: all functions included in the main function call chain.

[0044] It is understandable that after the first function generates the pseudo-exception marker corresponding to the first function, the pseudo-exception marker needs to be verified. Specifically: a global exception handling function is set in the main function call chain: the global exception handling function is a function set at the top level of the main function call chain that is specifically used to capture and handle exceptions. The global exception handling function covers all functions in the entire main function call chain. No matter where the exception is thrown, the global exception handling function will be triggered. The purpose of setting the global exception handling function is to uniformly handle any exceptions that may occur in the main function call chain, including the pseudo-exception marker generated by the first function.

[0045] The global exception handler is used to catch the pseudo-exception flag corresponding to the first function: when the first function generates a pseudo-exception flag due to the need to execute an asynchronous request, the pseudo-exception flag will be thrown immediately. The task of the global exception handler is to intercept the pseudo-exception flag and prevent it from interrupting the normal execution flow of the program as a real error.

[0046] Verify the function identifier and request type information in the pseudo-exception marker: A pseudo-exception marker should contain at least two key pieces of information: the function identifier and the request type. The function identifier uniquely identifies the asynchronous function that generated the pseudo-exception marker, while the request type contains details about the asynchronous request parameters, such as the request method and required data. The global exception handler must verify the correctness of both pieces of information.

[0047] Function ID Verification: Ensures that the function ID exists in a pre-defined function list, which includes all functions called in the main function call chain. If the verification passes, it means that the pseudo-exception flag indeed originated from a function in the call chain, increasing the credibility of the flag.

[0048] Request type information verification: Checks whether the request type information is complete, including ensuring that all required parameters are provided correctly. If the request type information is incomplete or contains errors, it may affect the correct execution of the asynchronous request, so verification is necessary.

[0049] Verification and subsequent processing: Only after the function identifier and request type information are verified will the global exception handling function determine that the pseudo-exception marker is valid and perform appropriate subsequent processing accordingly. For example, it may start an asynchronous event listener to track the asynchronous request status of the first function, or store the marker so that it can be correctly identified and utilized when the asynchronous request completes.

[0050] Optionally, before executing the call to the second function in the main function call chain in the above-mentioned step S204, the method further includes: obtaining function call data streams between multiple functions in the main function call chain, and determining the dependency relationship of the function calls of each function in the main function call chain based on the function call data stream; when it is determined according to the dependency relationship that the fourth function in the main function call chain does not depend on the asynchronous result, but depends on the synchronous result corresponding to the first function, determining whether the first function has completed the execution of the synchronous request, wherein the synchronous result is the result corresponding to the synchronous request; when it is determined that the first function has completed the execution of the synchronous request, determining the fourth function as the second function; and when it is determined according to the dependency relationship that the fifth function in the main function call chain does not depend on the asynchronous result, and does not depend on the synchronous result, determining the fifth function as the second function.

[0051] It is understandable that the second function can be identified by obtaining the function call data flow and identifying dependencies. In front-end development, the main function call chain is an execution path consisting of multiple function calls in sequence. Each function may depend on the output of the previous function, and these dependencies form the function call data flow. The purpose of obtaining the function call data flow is to understand the input-output relationship between functions in the entire main function call chain and their degree of dependence on the results of synchronous or asynchronous requests. By analyzing the function call data flow, a dependency graph is constructed. The dependency graph clearly shows the execution conditions of each function, including whether it needs to wait for the result of an asynchronous request.

[0052] Determine the execution conditions of the fourth function: When analyzing the fourth function, if it is found that the fourth function depends on the synchronous result of the first function, but does not depend on the asynchronous result that the first function may generate, it means that the fourth function can continue to execute in a synchronous manner after the first function completes the synchronous request. Therefore, it is possible to check whether the first function has completed the synchronous request. If the synchronous request of the first function has been completed and the synchronous result has been obtained, then the fourth function can be executed immediately without being blocked or converted into an asynchronous function. At this time, the fourth function can be the second function mentioned above;

[0053] Determine the execution conditions for the fifth function: If the fifth function depends neither on the asynchronous nor the synchronous results of the first function, this indicates that the fifth function executes completely independently of the first function. In this case, the fifth function can also be identified as the second function, meaning that the fifth function can execute directly synchronously without being affected by the asynchronous request of the first function.

[0054] Optionally, before executing the call to the third function in the main function call chain according to the asynchronous result in the above step S206, the method also includes: determining whether there is a call result corresponding to the sixth function in the main function call chain in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; if it is determined that there is no call result corresponding to the sixth function in the cache, the sixth function is determined as the third function.

[0055] It is understandable that the third function can be determined as follows: Before determining the third function, it is first necessary to understand the role of the cache in the embodiment of the present application: In functional programming, in order to improve performance and response speed, a cache mechanism is usually established in memory to store the results of function calls. When a function in the main function call chain is called again, the cache can be queried first to see if there is a previous call result of this function. If the result exists, the data in the cache can be directly returned, thereby avoiding repeated execution of the function and saving time and computing resources.

[0056] Analyze the calling status of the sixth function: As a function that may appear multiple times in the main function call chain, its execution efficiency directly affects the performance of the entire call chain. When encountering the sixth function, check whether the call result of the sixth function already exists in the cache.

[0057] Determine whether the sixth function needs to be executed: If the call result of the sixth function cannot be found in the cache, this means that there has been no previous call with the same input, or the result has been cleared. In this case, the sixth function needs to be executed to generate a new result and update the cache. In this case, the sixth function can be determined to be the third function.

[0058] Dynamically optimize call chains: By checking the cache before each call, it can determine which functions need to be executed and whose results can be directly obtained from the cache. This not only reduces unnecessary function calls but also speeds up program execution.

[0059] Optionally, the method also includes: when there are multiple first functions in the main function call chain that need to execute asynchronous requests, adding a global increment counter in the main function call chain; when any first function executes an asynchronous request, determining the current value of the global increment counter as the first identifier corresponding to any first function; when the first function after any first function executes the asynchronous request, updating the numerical value corresponding to the global increment counter to the sum of the current value and the target numerical value, and determining the sum as the second identifier corresponding to the first function after any first function; associating each identifier corresponding to each first function with each asynchronous result corresponding to each first function; when executing a call to the third function, determining the target identifier of the first function on which the third function depends, and obtaining the target asynchronous result associated with the target identifier, so as to execute the call to the third function according to the target asynchronous result.

[0060] It is understandable that in a scenario where there are multiple first functions that require asynchronous requests, the asynchronous requests can be managed through a global incrementing counter and identifier mechanism, specifically:

[0061] Introduction of a global incrementing counter: When there are multiple first functions (i.e., functions that need to execute asynchronous requests) in the main function call chain, a global incrementing counter can be introduced to track and manage the status of these asynchronous requests. The initial value of the global incrementing counter can be set to 0. Whenever a first function requiring asynchronous operation is encountered, the value of the global incrementing counter will be incremented. Through this method, a unique first identifier is assigned to each first function. The first identifier is associated with the asynchronous request that each first function needs to execute.

[0062] Association between first and second identifiers: When any first function executes an asynchronous request, the current global increment counter value is used as its first identifier. When another first function executes an asynchronous request after the previous first function, the value of the global increment counter is updated to the sum of the first identifier of the previous first function and a target value (which can be 1). The target value can be a constant or another variable. The new global increment counter value is determined as the second identifier of this subsequent first function.

[0063] Associating identifiers with asynchronous results: After each first function executes an asynchronous request and obtains a result, it associates the result with the corresponding identifier (the first identifier, the second identifier, or the identifier corresponding to another first function) and stores it in a result mapping table. The result mapping table ensures that the result of each asynchronous request can be correctly tracked and identified, even if multiple asynchronous requests are in progress simultaneously.

[0064] Ensuring the correct invocation of the third function: When preparing to execute the third function in the call chain, the system first determines which first function initiated the asynchronous request it depends on, thereby obtaining the target identifier of that first function. The system then searches the result mapping table for the target asynchronous result associated with that target identifier. Only after obtaining all necessary asynchronous results will the logic of the third function begin execution, ensuring that it can correctly process the complete set of asynchronous results.

[0065] Optionally, the above-mentioned step S202 of instructing the first function to generate a pseudo-exception mark corresponding to the first function includes: determining whether there is an asynchronous result corresponding to the first function in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; when it is determined that the asynchronous result exists in the cache, prohibiting the first function from executing the asynchronous request, and executing the call of each function in the main function call chain in sequence; when it is determined that the asynchronous request does not exist in the cache, instructing the first function to generate the pseudo-exception mark.

[0066] It's understandable that caching can be used to avoid repeated asynchronous requests, thereby optimizing the performance of the main function call chain in functional programming. Specifically, the cached asynchronous result is checked: Before the first function in the main function call chain (i.e., the function that needs to execute the asynchronous request) is executed, the cache is checked to see if the asynchronous result of the previous execution of this function already exists. This check is performed based on the unique identifier of the first function or its input parameters, ensuring the accuracy and validity of the result.

[0067] Avoiding duplicate asynchronous requests: If the asynchronous result corresponding to the first function already exists in the cache, this indicates that the same request has already been processed, so there is no need to execute the asynchronous request again. In this case, the result can be read directly from the cache, allowing other functions in the main function call chain to execute sequentially without being blocked by any asynchronous operations, maintaining the smoothness and continuity of the call chain and improving overall performance.

[0068] Generate a pseudo-exception flag: Conversely, if the asynchronous result of the first function is not found in the cache, this indicates that the request must be executed. However, to prevent the asynchronous operation from affecting other functions in the call chain, the first function can be instructed to generate a pseudo-exception flag. Then, the above steps S204 and S206 are executed.

[0069] In order to better understand the process of the above-mentioned function calling method, the implementation method flow of the above-mentioned function calling is described below in combination with an optional embodiment, but it is not used to limit the technical solution of the embodiment of this application.

[0070] In current functional programming, when a function that is called multiple times and in multiple places is changed to asynchronous due to changes in business or requirements, all other functions that call this function must also be changed to asynchronous, resulting in a large workload, a lot of repetitive and mechanical work, and prone to errors. The optional embodiment of the present application is to eliminate this contagion, and only modify the changed function without affecting other functions that call it. The optional embodiment of the present application provides a solution to eliminate the contagion of asynchronous functions in functional programming. Specifically:

[0071] Figure 3 Flowchart of a solution for eliminating the contagion of asynchronous functions in functional programming according to an optional embodiment of the present application. Figure 3 As shown:

[0072] Step S301: save the original fetch and redefine a new fetch.

[0073] Fetch (get, retrieve) is a standard Application Programming Interface (API) for initiating Hypertext Transfer Protocol (HTTP) requests. Before starting to modify the behavior of a fetch, you usually save the fetch in another variable.

[0074] After saving the original fetch, the global function fetch is redefined to add new features or modify its behavior to meet specific needs, such as handling asynchronous requests, adding logging, error handling, etc.

[0075] Step S302, the function starts.

[0076] That is, the normal execution of the process in the main function call chain begins, including: calling the main function, executing the function call of each function in the main function call chain, etc.

[0077] Step S303: Get asynchronous fetch (ie, the first function).

[0078] In a main function, function1->function2->function3->function4 are called in sequence. Function3 needs to perform an asynchronous request operation due to business changes, that is, function3 is an asynchronous fetch.

[0079] Step S304: Determine whether there is an asynchronous result in the cache.

[0080] If there is an asynchronous result, execute step S307; if there is no asynchronous result, execute step S305.

[0081] That is, when an asynchronous function is called, if there is a cache, the cached result is returned directly. If there is no cache, an error is thrown directly without waiting for the asynchronous function to return the result, ending the current call without blocking the normal calls of other functions and catching the thrown error through the catch method. In other words, in the custom fetch function, it is necessary to determine whether the fetch request can be retrieved from the cache array (i.e., the cache). If it can be retrieved, the cached data is returned; if not, the next logic step is performed.

[0082] Step S305: initiate asynchrony and throw an exception (ie, a pseudo exception flag).

[0083] Modify the function 3 code but do not block the js (a lightweight programming language) process with async / await. Instead, take advantage of the asynchronous nature of the js event loop, directly initiate a request, and add the request flag custom:true (a pseudo-exception flag).

[0084] Step S306: catch the exception and initiate a secondary call.

[0085] Redefine a run function. The function first declares the cache array cache and calls the asynchronous counting index i (the next call will fetch the corresponding cache based on this index). Then, it declares the source fetch request function to the variable ofetch and redefines its own fetch function.

[0086] Assign the corresponding index to the cache array as the cache content, then use the source fetch object ofetch to initiate a fetch asynchronous request and assign it to prom. Then, in the then function, assign the result to each field of result. Using the js event loop feature, the function will continue to execute. At this time, an exception will be thrown based on whether there is a custom field in the fetch parameter. If there is a custom field, a prom error will be thrown. Otherwise, prom will be returned.

[0087] After declaring and defining the above logic, the main code of the run function is executed through try / cache, that is, the main function is tried to be run and the exception is caught. At this time, when it is run for the first time, because there is no cache and the custom fetch function is executed, after detecting custom, a prom error will be thrown and the catch function will be entered. At this time, the catch function will judge the error type of the received error. If it is an instance of promise, it means that it is an asynchronous function, and here according to the then method of prom, when there is a returned result, the main function is rerun, and according to the calling order, the cached result can be obtained from the cache, so that the asyce / await without modifying the associated function is realized. The result of the asynchronous call is obtained by throwing an exception and re-executing, eliminating the infectiousness of the asynchronous function that needs to modify async / await when the associated function is called.

[0088] Step S307: Return the cached result.

[0089] When an asynchronous function is called, if there is a cache, the cached result is returned directly.

[0090] Step S308, function settlement.

[0091] After obtaining the cached data, normal function calls can be made based on the cached data, and asynchronous functions do not need to be processed asynchronously because the asynchronous results already exist in the cache.

[0092] In summary, the optional embodiment of the present application eliminates the contagious problem of asynchronous function calls in functional programming, and only modifies the changed function without affecting other functions that call the function, thereby reducing repetitive work and development workload.

[0093] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods of each embodiment of the present application.

[0094] Figure 4 is a structural block diagram of a function calling device according to an embodiment of the present application; Figure 4 Shown, including:

[0095] an indication module 42, configured to, when a first function that needs to execute an asynchronous request exists in a main function call chain, instruct the first function to generate a pseudo-exception marker corresponding to the first function, wherein the pseudo-exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request;

[0096] a first calling module 44, configured to, when the pseudo exception flag is captured, terminate the call to the first function and execute a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function;

[0097] The second calling module 46 is used to execute a call to the third function in the main function call chain according to the asynchronous result when it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0098] Through the above-mentioned device, when there is a first function in the main function call chain that needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark for indicating that the first function is executing and / or waiting to execute an asynchronous request; when the pseudo-exception mark is captured, the call to the first function is terminated, and the second function in the main function call chain that does not depend on the asynchronous result obtained by executing the asynchronous request of the first function is executed; when it is determined that the first function has completed the execution of the asynchronous request and generated an asynchronous result, the call to the third function in the main function call chain that depends on the asynchronous result is executed according to the asynchronous result. That is to say, in an embodiment of the present application, when the first function needs to execute an asynchronous request, the first function is instructed to generate a pseudo-exception mark for identifying that the first function is executing and / or waiting for an asynchronous operation; when a subsequent function (such as the second function and the third function) captures the pseudo-exception mark, the call to the first function is terminated, and then the second function that does not depend on the asynchronous result corresponding to the first function is executed. After the first function completes the asynchronous request and generates the asynchronous result, the third function can be called. Through the embodiments of the present application, after the pseudo-exception marker is captured, the call to the first function will be ended without being blocked by the asynchronous operation of the first function, thereby solving the problem of heavy workload of functional programming in the related art, in which when one function is changed to an asynchronous function, other functions that call the asynchronous function also need to be changed to asynchronous functions.

[0099] In an exemplary embodiment, the first calling module 44 is also used to create an asynchronous event listener corresponding to the first function, and monitor the execution status of the first function for the asynchronous request according to the asynchronous event listener; when it is determined by the asynchronous event listener that the execution status of the first function for the asynchronous request is completed, the asynchronous result is stored in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain.

[0100] In an exemplary embodiment, the indication module 42 is also used to set a global exception handling function in the main function call chain, wherein the global exception handling function is used to capture the pseudo-exception marker corresponding to the first function; when the global exception handling function captures the pseudo-exception marker, the function identifier and request type information in the pseudo-exception marker are verified, wherein the request type information is used to indicate the request parameters of the asynchronous request; when it is determined that the function identifier is a function identifier corresponding to a function included in a preset function list, it is determined that the function identifier verification is passed; and, when it is determined that the request type information is complete, it is determined that the request type information verification is passed, wherein the function list includes: all functions included in the main function call chain.

[0101] In an exemplary embodiment, the first calling module 44 is further used to obtain function call data flows between multiple functions in the main function call chain, and determine the dependency relationship of the function calls of each function in the main function call chain based on the function call data flows; when it is determined according to the dependency relationship that the fourth function in the main function call chain does not depend on the asynchronous result, but depends on the synchronous result corresponding to the first function, determine whether the first function has completed the execution of the synchronous request, wherein the synchronous result is the result corresponding to the synchronous request; when it is determined that the first function has completed the execution of the synchronous request, determine the fourth function as the second function; and when it is determined according to the dependency relationship that the fifth function in the main function call chain does not depend on the asynchronous result and does not depend on the synchronous result, determine the fifth function as the second function.

[0102] In an exemplary embodiment, the second calling module 46 is further used to determine whether a call result corresponding to the sixth function in the main function call chain exists in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; if it is determined that the call result corresponding to the sixth function does not exist in the cache, the sixth function is determined to be the third function.

[0103] In an exemplary embodiment, the device also includes: a counting module, which is used to add a global increment counter in the main function call chain when there are multiple first functions in the main function call chain that need to execute asynchronous requests; when any first function executes an asynchronous request, the current value of the global increment counter is determined as the first identifier corresponding to any first function; when the first function after any first function executes the asynchronous request, the numerical value corresponding to the global increment counter is updated to the sum of the current value and the target numerical value, and the sum is determined as the second identifier corresponding to the first function after any first function; each identifier corresponding to each first function is respectively associated with each asynchronous result corresponding to each first function; when executing a call to the third function, the target identifier of the first function on which the third function depends is determined, and the target asynchronous result associated with the target identifier is obtained, so as to execute the call to the third function according to the target asynchronous result.

[0104] In an exemplary embodiment, the indication module 42 is also used to determine whether there is an asynchronous result corresponding to the first function in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; when it is determined that the asynchronous result exists in the cache, the first function is prohibited from executing the asynchronous request, and the call of each function in the main function call chain is executed in sequence; when it is determined that the asynchronous request does not exist in the cache, the first function is instructed to generate the pseudo exception flag.

[0105] An embodiment of the present application further provides a storage medium, which includes a stored program, wherein the program executes any of the above methods when it is run.

[0106] Optionally, in this embodiment, the storage medium may be configured to store program codes for executing the following steps:

[0107] S1, when there is a first function that needs to execute an asynchronous request in the main function call chain, instructing the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request;

[0108] S2, when the pseudo exception flag is captured, ending the call to the first function and executing a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function;

[0109] S3. When it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, a call is executed to the third function in the main function call chain according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0110] An embodiment of the present application further provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any one of the above method embodiments.

[0111] Optionally, the electronic device may further include a transmission device and an input / output device, wherein the transmission device is connected to the processor, and the input / output device is connected to the processor.

[0112] Optionally, in this embodiment, the processor may be configured to execute the following steps through a computer program:

[0113] S1, when there is a first function that needs to execute an asynchronous request in the main function call chain, instructing the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request;

[0114] S2, when the pseudo exception flag is captured, ending the call to the first function and executing a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function;

[0115] S3. When it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, a call is executed to the third function in the main function call chain according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0116] An embodiment of the present application further provides a computer program product, including a computer program, which is used by a processor to execute the steps in any of the above method embodiments.

[0117] Optionally, in this embodiment, the computer program product may be processed by a processor to execute the following steps:

[0118] S1, when there is a first function that needs to execute an asynchronous request in the main function call chain, instructing the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request;

[0119] S2, when the pseudo exception flag is captured, ending the call to the first function and executing a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function;

[0120] S3. When it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, a call is executed to the third function in the main function call chain according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

[0121] Optionally, in this embodiment, the above-mentioned storage medium may include but is not limited to: a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk or an optical disk, and other media that can store program codes.

[0122] Optionally, specific examples in this embodiment may refer to the examples described in the above embodiments and optional implementation modes, and this embodiment will not be described in detail here.

[0123] Obviously, those skilled in the art should understand that the modules or steps of the present application described above can be implemented using a general-purpose computing device, they can be concentrated on a single computing device, or distributed on a network composed of multiple computing devices. Alternatively, they can be implemented using program code executable by the computing device, so that they can be stored in a storage device and executed by the computing device. In some cases, the steps shown or described can be performed in a different order than herein, or they can be made into separate integrated circuit modules, or multiple modules or steps can be made into a single integrated circuit module for implementation. Thus, the present application is not limited to any specific combination of hardware and software.

[0124] The above is only a preferred embodiment of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A function calling method, characterized in that: include: In a case where there is a first function that needs to execute an asynchronous request in the main function call chain, instructing the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request; When the pseudo exception flag is captured, terminate the call to the first function and execute a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function; When it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, a call to the third function in the main function call chain is executed according to the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

2. The function calling method according to claim 1, wherein: After the call to the first function ends, the method further includes: Creating an asynchronous event listener corresponding to the first function, and monitoring the execution status of the first function on the asynchronous request according to the asynchronous event listener; When it is determined through the asynchronous event listener that the execution status of the first function for the asynchronous request is completed, the asynchronous result is stored in the cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain.

3. The function calling method according to claim 1, wherein: After instructing the first function to generate a pseudo exception marker corresponding to the first function, the method further includes: Setting a global exception handling function in the main function call chain, wherein the global exception handling function is used to capture the pseudo exception flag corresponding to the first function; In a case where the global exception handling function captures the pseudo exception marker, verifying the function identifier and request type information in the pseudo exception marker, wherein the request type information is used to indicate request parameters of the asynchronous request; When it is determined that the function identifier is a function identifier corresponding to a function included in a preset function list, it is determined that the function identifier verification is passed, and when it is determined that the request type information is complete, it is determined that the request type information verification is passed, wherein the function list includes: all functions included in the main function call chain.

4. The function calling method according to claim 1, wherein: Before executing the call to the second function in the main function call chain, the method further includes: Obtaining function call data streams between multiple functions in the main function call chain, and determining the dependency relationship of the function calls of each function in the main function call chain based on the function call data stream; if it is determined according to the dependency relationship that a fourth function in the main function call chain does not depend on the asynchronous result but depends on the synchronous result corresponding to the first function, determining whether the first function has completed the execution of the synchronous request, wherein the synchronous result is the result corresponding to the synchronous request; if it is determined that the first function has completed the execution of the synchronous request, determining the fourth function as the second function; and, When it is determined according to the dependency relationship that the fifth function in the main function call chain does not depend on the asynchronous result and does not depend on the synchronous result, the fifth function is determined as the second function.

5. The function calling method according to claim 1, wherein: Before executing a call to a third function in the main function call chain according to the asynchronous result, the method further includes: Determining whether a call result corresponding to a sixth function in the main function call chain exists in a cache corresponding to the main function call chain, wherein the cache is used to store the call result corresponding to each function in the main function call chain; When it is determined that the calling result corresponding to the sixth function does not exist in the cache, the sixth function is determined to be the third function.

6. The function calling method according to claim 1, wherein: The method further comprises: In the case where there are multiple first functions in the main function call chain that need to execute asynchronous requests, adding a global increment counter in the main function call chain; In the case where any first function executes an asynchronous request, determining the current value of the global increment counter as the first identifier corresponding to the any first function; In a case where a first function after any of the first functions executes the asynchronous request, updating a value corresponding to the global increment counter to a sum of the current value and a target value, and determining the sum as a second identifier corresponding to the first function after any of the first functions; Associating each identifier corresponding to each first function with each asynchronous result corresponding to each first function; When executing a call to the third function, a target identifier of a first function on which the third function depends is determined, and a target asynchronous result associated with the target identifier is obtained to execute the call to the third function according to the target asynchronous result.

7. The function calling method according to claim 1, wherein: Instructing the first function to generate a pseudo exception marker corresponding to the first function includes: Determine whether an asynchronous result corresponding to the first function exists in a cache corresponding to the main function call chain, wherein the cache is used to store a call result corresponding to each function in the main function call chain; If it is determined that the asynchronous result exists in the cache, prohibiting the first function from executing the asynchronous request, and executing the call of each function in the main function call chain in sequence; If it is determined that the asynchronous request does not exist in the cache, the first function is instructed to generate the pseudo exception flag.

8. A function calling device, characterized in that: include: an indication module, configured to, when a first function that needs to execute an asynchronous request exists in a main function call chain, instruct the first function to generate a pseudo exception marker corresponding to the first function, wherein the pseudo exception marker is used to indicate that the first function is executing and / or is about to execute the asynchronous request; a first calling module, configured to, when the pseudo exception marker is captured, terminate the call to the first function and execute a call to a second function in the main function call chain, wherein the second function is a function that is independent of an asynchronous result obtained by executing the asynchronous request by the first function; The second calling module is used to execute a call to a third function in the main function call chain according to the asynchronous result when it is determined that the first function has completed executing the asynchronous request and generated the asynchronous result, wherein the third function is a function that depends on the asynchronous result.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored program, wherein the method according to any one of claims 1 to 7 is executed when the program is executed.

10. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method according to any one of claims 1 to 7 through the computer program.