How to combine two async "context.request" calls into one Action?

I need to make two server-side async context.request calls.

When I place each call in it’s own Action everything works as expected.

But when I combine both calls into one server-side Action it throws an Unexpected server error.

So why does bubble throw a server error when using two async context.request calls in the same action?

Here is working example where I call https://www.google.com and use the statusCode it returns. I’m returning the statusCode as a simple example, I don’t actually care what code it returns.

EXAMPLE 1: Two actions. Each action has it’s own server-side async “context.request” call. This works as expected.

The workflow:

Step 1 Action Code (“Test - Make First Call”):

function(properties, context) {
    
    //The first call using context.request
    const first_call = () => {
        let response = context.request("https://www.google.com");
        return response.statusCode;
    };
    
    //Do async calls and return the data
	return context.async( async callback => {
		try {
            let first_call_status = await first_call(); //Do this first and wait for a response
            let bubble_return_object = {
                first_call_status: first_call_status, //Outputs something from the first await
            };
            callback( undefined, bubble_return_object );
        }
        catch {
            callback (error);
    	}
    });
    
}

Step 2 Action Code (“Test - Add Second Call”):

function(properties, context) {
    
    const first_call_status = properties.first_call_status;
    
    //The second call using context.request
    const add_second_call = (first_call_status) => {
        let response = context.request("https://www.google.com");
        let combined_status = first_call_status + response.statusCode;
        return combined_status;
    };
    
    //Do async calls and return the data
	return context.async( async callback => {
		try {
            let combined_status = await add_second_call(first_call_status); //Then do this using the response
            let bubble_return_object = {
                combined_status: combined_status, //Outputs something from the second await
            };
            callback( undefined, bubble_return_object );
        }
        catch {
            callback (error);
    	}
    });
    
}

EXAMPLE 2: Two server-side async context.requests combined and placed inside one single action. This does NOT work. It throws an “Unexpected server error”.

The workflow:

Step 1 Action Code ("Test - Make First Call AND Add Second Call):

function(properties, context) {
    
    //The first call using context.request
    const first_call = () => {
        let response = context.request("https://www.google.com");
        return response.statusCode;
    };
    
    //The second call using context.request
    const add_second_call = (first_call_status) => {
        let response = context.request("https://www.google.com");
        let combined_status = first_call_status + response.statusCode;
        return combined_status;
    };
    
    //Do async calls and return the data
	return context.async( async callback => {
		try {
            let first_call_status = await first_call(); //Do this first and wait for a response
            let combined_status = await add_second_call(first_call_status); //Then do this using the response
            let bubble_return_object = {
                first_call_status: first_call_status, //Outputs something from the first await
                combined_status: combined_status, //Outputs something from the second await
            };
            callback( undefined, bubble_return_object );
        }
        catch {
            callback (error);
    	}
    });
    
}

Here is the console.log error:

And here is the server log error:

So why does it throw a server error when using two async context.request calls in the same action?

I figured it out.

It seems that context.async can’t handle multiple await's.

So the solution here was to add a third helper function to build the bubble_return_object, and call that third function inside context.async.

Here is the final code, hopefully it helps anyone else having this problem.

If anyone knows of a better solution please share.

function(properties, context) {
    
    //The first call using context.request
    const first_call = () => {
        let response = context.request("https://www.goggle.com");
        return response.statusCode;
    };
    
    //The second call using context.request
    const add_second_call = (first_call_status) => {
        let response = context.request("https://www.goggle.com");
        let combined_status = first_call_status + response.statusCode;
        return combined_status;
    };
    
    //Build the return object
    const get_return_object = () => {
        let first_call_status = first_call();
        let combined_status = add_second_call(first_call_status);
        let bubble_return_object = {
            first_call_status: first_call_status, //Outputs something from the first await
            combined_status: combined_status, //Outputs something from the second await
        };
        return bubble_return_object;
    };
    
    //Do the async call and return the data
	return context.async( async callback => {
		try {
            let bubble_return_object = get_return_object();
            callback( undefined, bubble_return_object );
        }
        catch {
            callback (error);
    	}
    });
    
}
6 Likes