我正在尝试将回调函数转换为Promise,
我为此使用util.promisify,以下是我的工作代码与新的不工作代码。
工作代码,使用节点回调样式-
let AWS = require(aws-sdk');
let moduleName = process.env.Module;
module.exports = {
getConstants
};
function getConstants (callback) {
let client = new AWS.SSM({
region: "us-east-1"
});
let smName = "/somePath";
let params = {
Names: [smName]
};
client.getParameters(params, function (err, data) {
if (err) {
console.log(err, err.stack);
callback(err, null);
}
else{
console.log(METHOD_TAG,'Parameter Store call successful!');
let constantVariables = data.Parameters[0].Value;
callback(null, constantVariables);
}
});
}
新的无效代码-
let AWS = require('aws-sdk');
let util = require('util');
let moduleName = process.env.Module;
module.exports.getConstants = async () => {
let client = new AWS.SSM({
region: "us-east-1"
});
let smName = "/somePath";
let params = {
Names: [smName]
};
let parameterStore = util.promisify(client.getParameters).bind(client);
let response = await parameterStore.getParameters(params);
let constantVariables = response.Parameters[0].Value;
return constantVariables;
};
我遇到以下错误-
TypeError: parameterStore.getParameters is not a function"
当尝试使AWS.SSM客户端的getParameters函数合理化时。
我在做错什么,我应该改变什么?
参考-
https://medium.com/@suyashmohan/util-promisify-in-node-js-v8-d07ef4ea8c53
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SSM.html#getParameters-property