39. WeChat Mini Program (API--Introduction, Operating Mechanism, Synchronous and Asynchronous)

WeChat Mini Program (API – Introduction, Operating Mechanism, Synchronous and Asynchronous)

Introduction to APIs

(1) What are APIs?

Citing Baidu Encyclopedia Introduction
insert image description here

(2) What is the WeChat Mini Program API?

API (Application Programming Interface, application programming interface), generally speaking: API is a kind of interface function, which encapsulates the function and gives it to developers, so that many functions do not need you to implement, as long as you can call it . WeChat applets use APIs to implement functions such as user information, data storage, and WeChat payment.

(3) Small program API structure

insert image description here

(4) API operation mechanism

Most API interfaces are written by the backend, and front-end developers make request calls. The popular understanding of api is to make something that can be used when you take it, which is convenient for our development.
For example, background data, by applying for the API interface, you can obtain the background data you want. These data are ready-made things, either you rent them from the Internet for five yuan, or they are provided to you by your back-end partners. In short You don't need to think about its production process, just take it and use it.

Case:
insert image description here
case analysis:
①var is to declare a variable, the variable name is the name we choose, and after the equal sign is a string, which is the address of the api interface. We obtain external data by this address, just like wanting to give When a person calls, the first thing is to know the other party's mobile phone number, then the variable we declared is equivalent to the other party's mobile phone number.
② Next, how to get the data we want from this api, this is about the wx.request network request API of the applet

Most API interfaces are written by the backend, and front-end developers make request calls

insert image description here
The above is a simple api operating mechanism.

(5) Synchronous and asynchronous

There are synchronous and asynchronous distinctions in the Mini Program API. Those with the suffix Sync are synchronous, and others are asynchronous.
For example, when clearing the cache API
insert image description here
involves the cache, we generally recommend using the synchronous writing method. The asynchronous writing method is actually a very dangerous writing method.
The first point is that it will make the code difficult to read;
the second point is that it will cause many unknown errors. If you are not very familiar with asynchronous methods, it is recommended to use synchronous methods if you can. Do not use asynchronous methods. In some cases in the program, it is necessary to use asynchronous, but it is very rare to use asynchronous in small programs.

(6) Synchronous VS Asynchronous

What are the disadvantages of synchronization?
When the method execution is very slow, the entire UI will be stuck, and the synchronous method cannot be run, and the subsequent methods cannot be executed. Therefore, the code will take a very long time, which is the disadvantage of synchronization.
What about the benefits of async?
The code will finish immediately, and after the cache is obtained, the framework will call success again, and the whole process will be completed quickly without causing the UI to stagnate, which is an advantage of asynchrony.

Summarize

Summary:
(1) API application programming interface, that is, Application Programming Interface, as long as you can call it
(2) API mechanism
(3) API synchronization Sync and asynchronous
PS Note: It is generally recommended to use synchronization when it can be used. Synchronization can not solve the problem and then use asynchronous

If you have any questions or questions, please leave a message to contact the editor! ! ! !

Thanks for visiting! ! !

Guess you like

Origin blog.csdn.net/weixin_45582846/article/details/107846077