# API

Mini Program development framework to provide rich micro-channel native API, you can easily tune up WeChat provides capabilities, such as access to user information, local storage, payment functions. Please refer to API file

Usually, in Mini Programs API There are several types:

# Event monitor API

We made a pact to on Initial API Used to listen if an event is triggered, such as:wx.onSocketOpenwx.onCompassChange Wait.

this kind API Accepts a callback function as a parameter that is called when an event is triggered, and passes in the relevant data as parameters.

Code examples

wx.onCompassChange(function (res) {
  console.log(res.direction)
})

# synchronization API

We made a pact to Sync Ending API It's all in sync. API, Such as wx.setStorageSyncwx.getSystemInfoSync Wait. In addition, there are some other synchronization API, such as wx.createWorkerwx.getBackgroundAudioManager For details, see API Instructions in the documentation.

synchronization API Can be obtained directly from the return value of the function, and an exception can be thrown if the execution goes wrong.

Code examples

try {
  wx.setStorageSync('key', 'value')
} catch (e) {
  console.error(e)
}

# asynchronous API

Majority API It's all asynchronous. API, such as wx.requestwx.login Wait. this kind API Interfaces usually accept a Object Type, which supports specifying the following fields on demand to receive interface call results

Object Parameter specification

Parameter name type Required Introductions
success function no Interface calls the successful callback function
fail function no Interface calls failed callback functions
complete function no Callback function at the end of an interface call (both successful and unsuccessful calls are executed)
Other Any - Additional parameters defined by the interface

Parameters to the callback function

successfailcomplete When the function is called, it passes in a Object Type parameter with the following fields:

attribute type Introductions
errMsg string Error message if the call returns successfully ${apiName}:ok
errCode number Error code, partial only API Support, please refer to the corresponding meaning API Documentation, if successful 0
Other Any Interface to return additional data

asynchronous API The need for the adoption of Object Gets the corresponding callback function passed in an argument of type. Partially asynchronous API There are also return values that can be used to implement richer functionality, such as wx.requestwx.connectSocket Wait.

Code examples

wx.login({
  success(res) {
    console.log(res.code)
  }
})

# asynchronous API return Promise

Base library 2.10.2 From version, asynchronous API Support callback & promise There are two ways to invoke. When interface parameters Object Object does not contain success/fail/complete Returns by default when Promise, otherwise still execute as callback, no return value.

# Note

  1. Partial interfaces such as downloadFile, request, uploadFile, connectSocket, createCameraThe game itself has a return value, Their promisify Need developer self-encapsulation.
  2. When there are no callback parameters, the asynchronous interface returns promise。 If the function call fails to enter fail Logic, Error prompt Uncaught (in promise), developers can use catch To capture.
  3. wx.onUnhandledRejection Can listen to unprocessed Promise Reject event.

Code examples

// callback Formal invocation
wx.chooseImage({
  success(res) {
    console.log('nothing:', nothing)
  }
})

// promise Formal invocation
wx.chooseImage().then(nothing => console.log('nothing: ', nothing))

# Cloud Development API

Access and useWeChat Cloud Development, you can use the cloud development API, in the Mini Program side directly call the server-side[Cloud function](https://developers.WeChat.qq.with/miniprogram/dev/wxcloud/guide/functions.html#Cloud function).

Code examples

wx.cloud.callFunction({
  // Cloud function name
  name: 'cloudFunc',
  // Parameters 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 calls