Skip to content

Instantly share code, notes, and snippets.

@jjasonclark
Last active July 1, 2020 14:15
Show Gist options
  • Save jjasonclark/283192301950c6e75b4cccb26b1c8ea6 to your computer and use it in GitHub Desktop.
Save jjasonclark/283192301950c6e75b4cccb26b1c8ea6 to your computer and use it in GitHub Desktop.
import AWS from 'aws-sdk';
const lambda = new AWS.Lambda();
const createAsyncInvokeParams = (
{
functionName,
payload
}
) => ({
InvocationType: 'DryRun',
FunctionName: functionName,
LogType: 'None',
Payload: JSON.stringify(payload)
});
export const promiseInvoke1 = params => new Promise((resolve, reject) => {
console.log('Starting promiseInvoke with ES6 promise wrapper');
lambda.invoke(createAsyncInvokeParams(params), (err, data) => {
console.log('inside callback for promiseInvoke1');
if (err == null) {
resolve(data);
} else {
reject(err);
}
});
});
export const promiseInvoke2 = params => {
console.log('Starting promiseInvoke with native promise');
return lambda.invoke(createAsyncInvokeParams(params)).promise();
};
export const promiseInvoke3 = params => new Promise((resolve, reject) => {
console.log('Starting promiseInvoke as a request object');
const request = lambda.invoke(createAsyncInvokeParams(params));
request.on('success', response => {
console.log('promiseInvoke success', response);
resolve(response.data);
});
request.on('error', response => {
console.log('promiseInvoke error', response);
reject(response.error);
});
request.on('complete', response => {
console.log('promiseInvoke complete', response);
resolve(response.data);
});
request.send();
});
export const promiseInvoke4 = (
{
functionName,
payload
}
) => new Promise((resolve, reject) => {
console.log(`Starting promiseInvoke InvokeAsync with ES6 promise wrapper - ${functionName}`);
lambda.invokeAsync(
{
FunctionName: functionName,
InvokeArgs: JSON.stringify(payload)
},
(err, data) => {
console.log('inside callback for promiseInvoke4');
if (err == null) {
resolve(data);
} else {
reject(err);
}
}
);
});
export const promiseInvoke5 = params => new Promise(resolve => {
console.log('Starting promiseInvoke5 as a request object without callbacks');
const request = lambda.invoke(createAsyncInvokeParams(params));
request.send();
resolve();
});
export const promiseInvoke6 = ({ functionName, payload }) => new Promise(resolve => {
console.log(`Starting promiseInvoke InvokeAsync without callback - ${functionName}`);
const request = lambda.invokeAsync({
FunctionName: functionName,
InvokeArgs: JSON.stringify(payload)
});
request.send();
resolve();
});
export const promiseInvoke = promiseInvoke2;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment