# API
Mini program development framework provides rich WeChat native API, can easily tune up WeChat provides capabilities, such as access to user information, local storage, payment functions, etc. Please refer to the API file。
Usually in Mini programs. API There are the following types:
# Event monitoring API
We agreed to, on
Beginning API Used to monitor whether an event is triggered, such as:[wx.onSocketOpen ]((wx.onSocketOpen )),wx.onCompassChange Etc.
this kind API Takes a callback function as an argument, which is called when the event fires, and passes in the relevant data as an argument.
Code Examples
wx.onCompassChange(function (res) {
console.log(res.direction)
})
# synchronization API
We agreed to, Sync
The ending API It's all in sync. API, Such as [wx.setStorageSync ]((wx.setStorageSync )),wx.getSystemInfoSync Etc. In addition, there are a number of other synchronization API, such as [wx.createWorker ]((wx.createWorker )),wx.getBackgroundAudioManager Etc., see details API Instructions in the documentation.
synchronization API The result of the execution of the function can be obtained directly through the return value of the function, and an exception will be thrown if the execution fails.
Code Examples
try {
wx.setStorageSync ('key', 'value')
} catch (e) {
console.error(e)
}
# asynchronous API
majority API Are asynchronous. API, such as wx.request,wx.login Etc. this kind API The interface usually accepts a Object
This parameter supports specifying the following fields on demand to receive the result of the interface call:
Object Dxplaination of parameters
Parameter name | type | Required | Introductions |
---|---|---|---|
success | function | no | Interface calls a successful callback function |
fail | function | no | Interface to call a failed callback function |
complete | function | no | The callback function at the end of the interface call (the call is executed on success or failure) |
Other | Any | - | Other parameters defined by the interface |
Parameters of the callback function
success
,fail
,complete
A function call is passed in Object
Type parameter, containing the following fields:
attribute | type | Introductions |
---|---|---|
errMsg | string | Error message if the call succeeds in returning ${apiName}:ok |
errCode | number | Error code, only partial API Support, please refer to the specific meaning of the corresponding API Document. On success, it is 0 。 |
Other | Any | Other data returned by the interface |
asynchronous API The results of the implementation need to be obtained through the Object
Gets the corresponding callback function passed in a parameter of type. Partially asynchronous API There will also be return values that can be used to implement richer functionality such as wx.request,wx.connectSocket Etc.
Code Examples
wx.login({
success(res) {
console.log(res.code)
}
})
# asynchronous API return Promise
Base library 2.10.2 Version, asynchronous API Support callback & promise Two ways of calling. When the interface parameter Object Object does not contain success/fail/complete Will return by default Promise, otherwise it is still executed by callback, no return value.
# Note
- Part of the interface such as
downloadFile
,request
,uploadFile
,connectSocket
,createCamera
(small game) itself has a return value, Their promisify Developers need to package themselves. - When there is no callback parameter, the asynchronous interface returns promise。If the function call fails to enter fail Logic, Will report error message
Uncaught (in promise)
, the developer can pass catch For the capture. - wx.onUnhandledRejection Can listen on the unprocessed Promise Reject the event.
Code Examples
// callback Formal call
wx.chooseImage({
success(res) {
console.log('res:', res)
}
})
// promise Formal call
wx.chooseImage().then(res => console.log('res: ', res))
# Cloud development API
Open and use[WeChat cloud development](https://developers.weixin.qq.com/miniprogram/dev/wxcloud /basis/getting-started.html), you can use the cloud development API, in the Mini Program side directly call the server[Cloud function](https://developers.weixin.qq.com/miniprogram/dev/wxcloud /Guide/functions.html#Cloud function)。
Code Examples
wx.cloud.callFunction({
// Cloud Function Name
name: 'cloudFunc',
// Parameters passed to the cloud function
data: {
a: 1,
b: 2,
},
success: function(res) {
console.log(res.result) // Example
},
fail: console.error
})
// In addition, cloud functions also support promise form calls