aws-amplify / amplify-js

A declarative JavaScript library for application development using cloud services.
https://docs.amplify.aws/lib/q/platform/js
Apache License 2.0
9.42k stars 2.12k forks source link

Amplify.record function not firing the .then() #12221

Closed ThisisBada closed 10 months ago

ThisisBada commented 11 months ago

Before opening, please confirm:

JavaScript Framework

React Native

Amplify APIs

Analytics

Amplify Categories

analytics

Environment information

``` # Put output below this line System: OS: macOS 13.6 CPU: (8) arm64 Apple M1 Pro Memory: 47.89 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 20.6.1 - /usr/local/bin/node Yarn: 1.22.19 - /usr/local/bin/yarn npm: 9.5.1 - /opt/homebrew/bin/npm Browsers: Chrome: 117.0.5938.149 Safari: 17.0 npmPackages: @babel/core: ^7.20.0 => 7.22.11 @react-native-async-storage/async-storage: 1.18.2 => 1.18.2 @react-native-community/netinfo: 9.3.10 => 9.3.10 HelloWorld: 0.0.1 aws-amplify: ^5.3.11 => 5.3.11 expo: ~49.0.8 => 49.0.9 expo-status-bar: ~1.6.0 => 1.6.0 react: 18.2.0 => 18.2.0 react-native: 0.72.5 => 0.72.5 npmGlobalPackages: @angular/cli: 16.0.5 @aws-amplify/cli: 12.4.0 @vue/cli: 5.0.8 aws-cdk: 2.96.2 corepack: 0.19.0 create-js-project: 1.0.0 create-nuxt-app: 5.0.0 create-react-app: 5.0.1 create-vanilla-app: 1.1.5 expo-cli: 6.3.0 gulp: 4.0.2 next: 13.5.4 npm-check-updates: 16.10.12 npm: 9.8.1 npmvet: 0.2.1 rimraf: 5.0.1 yarn: 1.22.19 ```

Describe the bug

Hello Team,

When using Analytics Category of Amplify. Specifically, when using the Analytics.record() function, it is mentioned here that it returns a promise and it is confirmed here that it is an async function.

The issue is that the Analytics.record() does not fire the .then() when I call it.

Note : in Amplify docs, it does not mention if the Analytics.record() is sync or async but there is no await in the doc's code so I assumed that it is sync then I went to the code and I saw that it is async.

Please find below the code that I used :

        console.log("After call analytics");
        Analytics.record(
          {
            streamName: "awesomeprojectKinesis-dev",
            data: {
              payload: myPayLoad,
            },
          },
          "AWSKinesis"
        )
          .then(() => {
            console.log("Then Block");
          })
          .catch((e) => {
            console.log(e);
          });
        console.log("After call analytics");

This will log only :

 LOG  Before call analytics
 LOG  After call analytics

It will not log "Then Block"

Expected behavior

The expected behaviour is that the Analytics.record() function triggers the .then()

Reproduction steps

  1. Unzip the attached zip file
  2. Open it in your favourite code editor
  3. npm install
  4. npm start (to test if project is working) I test with iOS
  5. amplify init
  6. amplify add analytics
  7. npm start > i for iOS
  8. Click the "Click Me to record" BugAnalytics.zip

Note : I only included the amplify folder for your reference.

Code Snippet

// Put your code below this line.

Log output

``` // Put your logs below this line ```

aws-exports.js

/* eslint-disable */
// WARNING: DO NOT EDIT. This file is automatically generated by AWS Amplify. It will be overwritten.

const awsmobile = {
    "aws_project_region": "eu-west-1",
    "aws_cognito_identity_pool_id": "eu-west-1:702eede6-1704-4e12-b430-0be16d79a6ae",
    "aws_cognito_region": "eu-west-1",
    "aws_user_pools_id": "eu-west-1_HyJV99SQa",
    "aws_user_pools_web_client_id": "3skihr0dsg51p7sfihast6b0au",
    "oauth": {},
    "aws_cognito_username_attributes": [],
    "aws_cognito_social_providers": [],
    "aws_cognito_signup_attributes": [
        "EMAIL"
    ],
    "aws_cognito_mfa_configuration": "OFF",
    "aws_cognito_mfa_types": [
        "SMS"
    ],
    "aws_cognito_password_protection_settings": {
        "passwordPolicyMinLength": 8,
        "passwordPolicyCharacters": []
    },
    "aws_cognito_verification_mechanisms": [
        "EMAIL"
    ]
};

export default awsmobile;

Manual configuration

No response

Additional configuration

No response

Mobile Device

Simulator iPhone 15 pro max

Mobile Operating System

iOS 17

Mobile Browser

No response

Mobile Browser Version

No response

Additional information and screenshots

No response

cwomack commented 11 months ago

Hello, @ThisisBada 👋 and thanks for opening this issue. In v5.X of Amplify, theAmplify.record() method appears to be an async function but does not ever resolve. We realize that this is not ideal and have addressed this discrepancy in our next major version, v6 of Amplify JS. We actually just released the developer preview for it, which includes Analytics within the categories that can currently be used!

While the documentation does not specify that you should await this function, we consider the Analytics record API to be one that ought to be synchronous and non-blocking for our customers' applications. Since you're on v5.X at this time, please follow the recommendations on the documentation and treat the function as synchronous. It should work as a "fire and forget" model.

Let us know if you have any further questions on the subject!

cwomack commented 10 months ago

With the docs issue opened to track this, I'll close this issue. Thank you for creating this issue to help us make a needed improvement in our documentation!