How can we access input values in the then block of context promise in JavaScript? -
how can access input values in block of context promise in javascript? see code snippet below. processinserver() may invoked several times.
function processinserver(somedata){ return getpromise(somedata).then(function(data) { return {"input": somedata, "output": data}; }); } var a1 = processinserver(somedata1); var a2 = processinserver(somedata2);
you adjust getpromise
constructor returns object. set somedata
, promise
properties of returned object. somedata
should accessible @ object reference, promise
object. use .then()
chained promise set @ object review both promise value , input value.
function getpromise(somedata) { this.somedata = somedata; this.promise = promise.resolve(somedata * math.random() * math.pi); } var gp = new getpromise(123); var result = gp.promise.then(function(data) { return { "input": gp.somedata, "output": data }; }); result.then(function(result) { console.log(`result:${json.stringify(result, null, 2)}`, `gp:${json.stringify(gp, null, 2)}`) });
Comments
Post a Comment