grpc-Nodejs同步异步async/await

使用async/await做同步处理
ES6已经内置支持async和await


var client = require("../../client/greeter_client.js");
var pubfunc = require("../../public/javascripts/pubfunc.js");
var message = require('../../client/generate/secondary_password_check_pb.js');

function secondary_password_check(userName, password) {
    return new Promise((resolve, reject)=>{
        try {
            var request = new message.reqData();
            request.setUserName(userName);
            var decodePassword = Buffer.from(password, 'base64').toString('ascii');
            request.setPassword(decodePassword);

            client.secondaryPasswordCheck(request, { deadline: pubfunc.getClientTimeout() }, function (err, response) {
                if (err) {
                    console.log(err);
                    return false;
                }

                resolve(response.toObject().result);
            });
        } catch (error) {
            reject(error);
        }   
    })    
};

module.exports = async function (userName, password) {
    var result = await secondary_password_check(userName, password);
    return result;
}

resolve可以将想要的结果返回
async用于声明同步函数
await表示此函数将同步执行,得到的结果为Promise

你可能感兴趣的:(grpc-Nodejs同步异步async/await)