Wx.request

I saw the code written by someone:

wx.request.post().then();

that"s what he wrote about renquest. Why is it followed by a. Then ();
. Then it can"t be used in ordinary functions? Is it possible for him to use it like this only after he has made it easier to use it?

Apr.11,2021

indicates that the return value of this function is a Promise object


I use the following to implement Promise :
promisify.js

module.exports = (api) => {
    return (options, ...params) => {
        return new Promise((resolve, reject) => {
            api(Object.assign({}, options, { success: resolve, fail: reject }), ...params);
        });
    }
}

then wrap wx.request

const promisify = require('./promisify.js')
request = promisify(wx.request)

request({
    method: 'POST',
        url: url,
        data: data,
        header: {
            'content-type': 'application/x-www-form-urlencoded' //application/json
        },
}).then(...)

A post with success and fail can be encapsulated here, so that method and header

can be avoided every time.
Menu