0

Currently, the AWS documentation has a function that looks like

describeInstances(params = {}, callback) ⇒ AWS.Request

My code for using it looks like

let getAwsInstancesInfo = function () {
    console.log("getting AWS Instances information");
    return ec2.describeInstances(function (err, data) {
        if (err) {
            console.log("error in receiving instance information from EC2", err.stack);
            return Promise.reject("error in receiving instance information from EC2");
        } else {
            console.log("Total instances from EC2:", Object.keys(data["Reservations"]).length);
            return Promise.resolve(data);
        }
    });
};

On the calling side, I do

getAwsInstancesInfo().then(data => {
    console.log("Total instances from EC2:", Object.keys(data["Reservations"]).length);
});

However, it fails saying the following

index.js:12 Uncaught TypeError: Object(...)(...).then is not a function
    at InstanceSummary.componentDidMount (index.js:12)
    at ReactCompositeComponent.js:264
    at measureLifeCyclePerf (ReactCompositeComponent.js:75)
    at ReactCompositeComponent.js:263
    at CallbackQueue.notifyAll (CallbackQueue.js:76)
    at ReactReconcileTransaction.close (ReactReconcileTransaction.js:80)
    at ReactReconcileTransaction.closeAll (Transaction.js:209)
    at ReactReconcileTransaction.perform (Transaction.js:156)
    at batchedMountComponentIntoNode (ReactMount.js:126)
    at ReactDefaultBatchingStrategyTransaction.perform (Transaction.js:143)
    at Object.batchedUpdates (ReactDefaultBatchingStrategy.js:62)
    at Object.batchedUpdates (ReactUpdates.js:97)
    at Object._renderNewRootComponent (ReactMount.js:319)
    at Object._renderSubtreeIntoContainer (ReactMount.js:401)
    at Object.render (ReactMount.js:422)
    at Object../src/index.js (index.js:21)
    at __webpack_require__ (bootstrap 0a6128d5488129446b34:669)
    at fn (bootstrap 0a6128d5488129446b34:87)
    at Object.0 (registerServiceWorker.js:108)
    at __webpack_require__ (bootstrap 0a6128d5488129446b34:669)
    at bootstrap 0a6128d5488129446b34:715
    at bundle.js:719

I am using React.js and my package.json looks like

{
  "name": "myproject",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "aws-sdk": "^2.105.0",
    "material-ui": "^0.19.0",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-scripts": "1.0.12"
  },
  "scripts": {
    "api": "node src/api/index.js",
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • You'd have to use `new Promise` - promises don't change the fact that a `return` inside that callback is pointless. But really just use the builtin promise support :-) – Bergi Aug 31 '17 at 00:18

2 Answers2

2

https://aws.amazon.com/ko/blogs/developer/support-for-promises-in-the-sdk/

AWS.Request object supports promise() method. See this document.

kangsu
  • 226
  • 1
  • 6
1

Based on @Kangsu help, all I had to do was

let getAwsInstancesInfo = function () {
    return ec2.describeInstances().promise();
};

That's it. Thank you @kangsu very much!

daydreamer
  • 87,243
  • 191
  • 450
  • 722