gpt4 book ai didi

javascript - 同步链接 Promise

转载 作者:行者123 更新时间:2023-12-03 03:57:18 24 4
gpt4 key购买 nike

我正在使用 AWS Lambda。我正在尝试解密异步函数中的 key ,然后向另一个 URL 发出 POST 请求,这取决于首先从第一次调用中获取 key 。

module.exports = function (payload) {
return new Promise(function(resolve, reject) {
var headers = config.Headers;
decrypt(headers.Authorization)
.then(function (auth_token) {
headers.Authorization = 'Basic ' + auth_token;
console.log('dispatch got the key from kms');
return makePostCall(headers, payload);
})
.then(function (changeNumber){
console.log(changeNumber);
return resolve(changeNumber);
})
.catch (function (error) {
console.error('Error during dispatch: ' + error);
return reject(error);
});
});
};

decryptmakePostCall 调用均返回 rejectresolve。在本地运行良好,但在 Lambda 上运行时,仅成功运行几次,这让我相信问题出在 makePostCall 函数的异步调用上。我得到的错误(来自 catch)是:

Error during dispatch: null

我需要先运行解密->获取 key ->然后MakePostCall。

编辑: makePostCall 如下所示:

function makePostCall(headers, payload) {
return new Promise(function (resolve, reject) {
const url = config.serviceNowEndpoint + config.serviceNowChangeUri;
request.post({
url: url,
method: 'POST',
headers: headers,
json: payload
}, function (error, response, body) {
if (!error) {
return resolve(body.change_request.number);
}
else {
return reject(new Error('Returned with status code: ' + response.statusCode));
}
});
});
}

更多编辑:根据@Jaromanda X的建议,将代码修改为:

module.exports = function (payload) {
var headers = config.Headers;
return decrypt(headers.Authorization)
.then(function (auth_token) {
headers.Authorization = 'Basic ' + auth_token;
console.log('dispatch got the key from kms');
return makePostCall(headers, payload);
})
.catch (function (error) {
console.error('Error during dispatch: ' + error);
return error;
});
};

问题仍然存在。在本地运行良好,但在 Lambda 上异步

编辑添加解密代码:

const AWS = require('aws-sdk');
const config = require('../config/config');

module.exports = function(token) {
return new Promise(function (resolve, reject) {
const kms = new AWS.KMS({ region: config.aws_region.region});

const params = {
CiphertextBlob: new Buffer(token, 'base64')
};
kms.decrypt(params, function (err, data) {
if (!err) {
console.log('decrypted successfully');
return resolve(data.Plaintext.toString());
} else {
return reject(`${err.message}`);
}
});
});
};

最佳答案

这是一个 AWS lambda 错误。请参阅issue at Bluebird's repobasic reproducing code对于这个问题。

这个问题实际上与 bluebird 无关,而是与 lambda 部署上的异步代码处理和调度有关。 https://github.com/nervous-systems/cljs-lambda/issues/62 。这是重现该错误的另一个问题:https://github.com/nervous-systems/cljs-lambda/issues/62

用户提出了一个常见问题:

Oh my god. Just in case anyone else lands here and makes my mistake: make sure you haven't accidentally deployed AWS.config.update(...) using a localhost endpoint instead of an actual AWS region-specific endpoint. That can present with similar symptoms as above (say, if the only async function you're using is accessing a DynamoDB document client) but for utterly different reasons.

关于javascript - 同步链接 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44884409/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com