Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support of waitForMultipleEvents #1315

Open
charanjit-singh opened this issue May 1, 2024 · 1 comment
Open

feat: Add support of waitForMultipleEvents #1315

charanjit-singh opened this issue May 1, 2024 · 1 comment
Assignees
Labels
feature New feature or request

Comments

@charanjit-singh
Copy link

Is your feature request related to a problem? Please describe.
Yes, I want to wait for 2 events and continue any of the two events is received. If no event is received then longest of both timeouts should hit and I should get null.

Describe the solution you'd like
want to wait for 2 events and continue any of the two events is received. If no event is received then longest of both timeouts should hit and I should get null.

Describe alternatives you've considered

Following code is not going to work because promises don't work in inngest as normal promises and are not allowed.

const waitForStampUploaded = async () => {
    return new Promise((resolve, reject) => {
        // Wait for 1000 ms
        setTimeout(() => {
            resolve({
                "data": {
                    "status": "success",
                }
            });
        }, 1000);
    });
}


const waitForStampSkipped = async () => {
    return new Promise((resolve, reject) => {
        // Wait for 1000 ms
        setTimeout(() => {
            resolve({
                "data": {
                    "status": "skipped",
                }
            })
        }, 1200);

        setTimeout(() => {
            resolve(null)
        }, 1000);
    });
}


const stampStatus = () => {
    // Wait for stampUploaded promise or stampSkipped promise
    // If any of the promise is resolved with not null value, return the value
    // If any of the promise is resolved with null value, wait for the other promise to 
    // Check if it is resolved with null value or not
    // If both are null then return null
    // If any of the promise is resolved with not null value, return the value
    // We must wait for both the promises to resolve in parallel


    return new Promise((resolve) => {   

        let stampUploadedPromise = waitForStampUploaded();
        let stampSkippedPromise = waitForStampSkipped();

        let isStampUploadedResolved = false;
        let isStampSkippedResolved = false;

        stampUploadedPromise.then((response) => {
            isStampUploadedResolved = true;
            if (response != null) {
                resolve(response);
            }
            else {
                if(isStampSkippedResolved) {
                    resolve(null);
                }
            }
        });

        stampSkippedPromise.then((response) => {
            isStampSkippedResolved = true;
            if (response != null) {
                resolve(response);
            } else {
                if(isStampUploadedResolved) {
                    resolve(null);
                }
            }
        });

    });

}


/**
--------------------------------
| success | skip | output |
--------------------------------
| 0       | 1    |  1     |
| 1       | 0    |  1     |
| 1       | 1    |  1     |
| 0       | 0    |  0     |
--------------------------------
 */
@charanjit-singh charanjit-singh added the feature New feature or request label May 1, 2024
Copy link

linear bot commented May 1, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants