Friday, June 19, 2020

[Promise][Resolved] Get data from promise, use in another function

I would like to use result return from Promise in another function.
Source:
let cleanDbObj = async function(){
  var params = {
    TableName: "products"
  };
  return await dynamodb.scan(params).promise().then((data)=>{
      var result = {};
      result.Count = data.Count;
      result.ScannedCount = data.ScannedCount;
      result.Items = data.Items.map((item)=>{
        return {
          id: item.id.N,
          name: item.name.S,
          type: item.type.L,
        };
      });
    return result;
  })
}
However, if I print result of cleanDBObj, I can get a "Promise {<pending>}" with "Value below was evaluated just now information". and when I call this value content ,
these are undefined.

I cannot found a solution which satisify me but method from codehandbook.org can do sth similiar to what i want:
async function getResult(){
  let result = await cleanDynamoDbObj();
  alert(Json.stringify(result));
}
getResult()

Reference:
https://codehandbook.org/how-to-return-data-from-javascript-promise-es6/
https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Array/map

No comments :

Post a Comment