JavaScript 中编写同时支持 PromiseCallback 语法的函数调用;

同时支持 Promise 调用也支持 Callback 异步回调的函数,假定其调用方式规定如下:

var client = new CNodeJS({
    url: 'http://127.0.0.1:3000/hw/',
});
// 
client.request(params)
    .then(function(data){
    })
    .catch(function(err){
    });
//
client.request(params, function callback(err, data){
});

1.1. 函数定义

'use strict';

var request = require('request');

class CNodeJS {
    constructor(options) {
        this.options = options||{};
        options.url = options.url || "http://127.0.0.1:3000/hw/";
    }

    baseParams(params) {
        params = Object.assign({}, params || {});
        if(this.options.token){
            params.accesstoken = this.options.token;
        }
        return params;
    }

    request1(method, path, params) {
        return new Promise((resolve, reject) => {
            var opts = {
                method : method.toUpperCase(),
                url : this.options.url + path,
                json : true,
            };

            if(opts.method === 'GET' || opts.method === 'HEAD') {
                opts.qs = this.baseParams(params);
            }else{
                opts.body = this.baseParams(params);
            }

            request(opts, (err, res, body) => {
                if(err){
                    return reject(err);
                }

                if(body){
                    // body || res.body
                    resolve(body);
                }else{
                    reject(new Error("body#error"));
                }
            });
        });
    }

    request2(method, path, params, callback) {
        return new Promise((resolve_, reject_) => {
            var opts = {
                method : method.toUpperCase(),
                url : this.options.url + path,
                json : true,
            };

            if(opts.method === 'GET' || opts.method === 'HEAD') {
                opts.qs = this.baseParams(params);
            }else{
                opts.body = this.baseParams(params);
            }

            const resolve = res => {
                resolve_(res);
                callback && callback(null, res);
            };

            const reject = err => {
                reject_(err);
                callback && callback(err);
            }

            request(opts, (err, res, body) => {
                if(err){
                    return reject(err);
                }

                if(body){
                    // body || res.body
                    resolve(body);
                }else{
                    reject(new Error("body#error"));
                }
            });
        });
    }

    request3(method, path, params, callback) {
        return request2(method, path, params, callback)
            .then(result => Promise.resolve(result.data));
    }
}

var client = new CNodeJS({
    url: 'http://127.0.0.1:3000/hw/',
});

// 仅支持 Promise
client.request1('GET', 'abc', {})
    .then(result => console.log(result))
    .catch(err => console.error(err));

// 支持 Promise 和 Callback
client.request2('GET', 'abc', {}, function callback(err, res) {
    // body...
    if(err){
        console.error(err);
    }else{
        console.log("callback:");
        console.log(res);
    }
});

// 请求3 => 转换服务器返回的数据
client.request3('GET', 'dbc', {})
    .then(result => console.log(result))
    .catch(err => console.log(err));

link link

results matching ""

    No results matching ""