node.js - How do you call a function with parameters in a .then function in a JavaScript promise string? -


i converting aws lambda functions, written in node.js, use promises instead of callbacks. i'm wrapping of functions in handler handler code. i'm trying break out simple functions can have flat promise chain possible in handler code.

i'm stuck @ 1 point have .then() returns value need pass 1 of functions, has been promisified, along other parameters. have searched high & low can't find example of syntax this. i'm not sure i'm doing right thing @ all. of articles i've found explain simple promise chains return value through .then() method. none pass promisified function.

here's have far:

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";       // gets token used parameter 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 token: "+error);         });     }      function getdata(userid, db, token){          var qparams = {             // params 1 record          };         return dynamodb.querypromised(qparams)         .then(function (data){             var start_date = // did date manipulation on data value             // request records, passing token in 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 parameters};             rp.get({                 url:url,                  qs:tokenparams,                  headers:headers,                  followredirect: true             }).then(function(body){                 return body;             }).catch(function(error){                 console.log("could not data: "+error);             });         }).catch(function(error){             console.log("final catch - getdata failed: "+error);         });     }      // handler code starts      // array of userids data     dynamodb.scanpromised({         // params user ids     }).then(function(users){         for(var i=0; i<users.length; i++){             userid = // value user record;                     // request token             var tokenparams = {an object of params};             gettoken(tokenparams)             .then(function(token){             ///////////// need /////////////////             /* there way pass getdata token within .then() don't have nested promise? */                 getdata(userid, users[i].dbname, token)              //////////////////////////////////////////////////////////             }).catch(function (e){                 console.log("caught error");             });         }     }).catch(function (e){         console.log("caught error");     }); }; 

you can use promise.all(), .then(), function.prototype.bind(); return rp.get() getdata()

 return promise.all(users.map(function(user) {    userid = // value user record;              // request token      var tokenparams = {        object of params      };    return gettoken(tokenparams)      .then(getdata.bind(null, userid, user.dbname))      .catch(function(e) {        console.log("caught error");        throw e      });  })) 

Comments

Popular posts from this blog

sql server - Cannot query correctly (MSSQL - PHP - JSON) -

php - trouble displaying mysqli database results in correct order -

C++ Linked List -