我正在转换用 node.js 编写的 AWS lambda 函数,以使用 promise 而不是回调。我用处理程序代码将我的所有函数包装在处理程序中。我试图分解简单的函数,这样我就可以在处理程序代码中拥有尽可能平坦的 promise 链。
我被困在一个点上,我有一个 .then() 返回一个值,我需要将这个值连同其他参数一起传递给我的一个函数,该函数已被 promise 。我搜索了 high & low 但找不到执行此操作的语法示例。我什至不确定我在做什么是正确的。我找到的所有文章都解释了仅通过 .then() 方法返回值的简单 promise 链。 None 然后将其传递给另一个 promisified 函数。
这是我目前所拥有的:
var bbPromise = require("./node_modules/bluebird");
var AWS = require("./node_modules/aws-promised");
var rp = require("./node_modules/request-promise");
var moment = require('./node_modules/moment.js');
var dynamodb = new AWS.dynamoDb();
exports.handler = function(event, context) {
"use-strict";
// This gets a token that will be used as a parameter for a request
function getToken(params){
return rp.post({
url: "https://api.something.com/oauth2/token",
followRedirects: true,
form: params,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}).then(function(body){
return JSON.parse(body).access_token;
}).catch(function(error){
console.log("could not get token: "+error);
});
}
function getData(userId, db, token){
var qParams = {
// params that will get one record
};
return dynamodb.queryPromised(qParams)
.then(function (data){
var start_date = // did some date manipulation on data to get this value
// Request records, passing the token in the header
var url = "https://api.something.com/data/?db="+db+"&start_date="+start_date;
var headers = {
'Content-Type': 'application/x-www-form-urlencoded',
'Authorization':'Bearer '+token
};
tokenParams = {all the parameters};
rp.get({
url:url,
qs:tokenParams,
headers:headers,
followRedirect: true
}).then(function(body){
return body;
}).catch(function(error){
console.log("could not get data: "+error);
});
}).catch(function(error){
console.log("Final Catch - getData failed: "+error);
});
}
// THIS IS WHERE THE HANDLER CODE STARTS
// Get an array of all userIds then get their data
dynamodb.scanPromised({
// params that will get the user Ids
}).then(function(users){
for(var i=0; i<users.length; i++){
userId = // the value from the user record;
// Request a token
var tokenParams = {an object of params};
getToken(tokenParams)
.then(function(token){
///////////// THIS IS WHERE I NEED HELP /////////////////
/* Is there a way to pass getData the token within the .then() so I don't have a nested promise? */
getData(userId, users[i].dbName, token)
//////////////////////////////////////////////////////////
}).catch(function (e){
console.log("caught an error");
});
}
}).catch(function (e){
console.log("caught an error");
});
};
最佳答案
你可以使用Promise.all()
, .then()
, Function.prototype.bind()
; 返回
rp.get()
来自getData()
return Promise.all(users.map(function(user) {
userId = // the value from the user record;
// Request a token
var tokenParams = {
an object of params
};
return getToken(tokenParams)
.then(getData.bind(null, userId, user.dbName))
.catch(function(e) {
console.log("caught an error");
throw e
});
}))
关于javascript - 如何在 JavaScript promise 字符串的 .then 函数中调用带参数的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40538284/