Skip to content

Instantly share code, notes, and snippets.

View AshanthaLahiru's full-sized avatar
🎯
Focusing

Ashantha Lahiru AshanthaLahiru

🎯
Focusing
View GitHub Profile
'use strict';
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message: 'Go Serverless v1.0! Your function executed successfully!',
input: event,
}),
};
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
@AshanthaLahiru
AshanthaLahiru / typical_promise.js
Last active April 29, 2018 11:49
Typical promise
let promise = (time) => {
return new Promise((res, rej) => {
console.log(`Promise ${time} start`)
setTimeout(() => {
res(`Promise ${time} end`)
}, time)
})
}
@AshanthaLahiru
AshanthaLahiru / promise_performance1.js
Last active April 29, 2018 11:50
Performance of promise
promise(1000) // |-----|
.then(msg => {
console.log(msg)
})
promise(2000) // |-----|-----|
.then(msg => {
console.log(msg)
})
promise(3000) // |-----|-----|-----|
.then(msg => {
@AshanthaLahiru
AshanthaLahiru / promise_performance2.js
Last active April 29, 2018 11:53
Promise performance
promise(1000) // |-----|
.then((msg) => {
console.log(msg)
return promise(2000) // |-----|-----|
})
.then((msg) => {
console.log(msg)
return promise(3000) // |-----|-----|-----|
})
.then((msg) => {
@AshanthaLahiru
AshanthaLahiru / promise_performance3.js
Last active April 29, 2018 11:56
Promise performance
Promise.all([promise(1000), promise(2000), promise(3000)])
.then((msgs) => {
msgs.forEach(msg => {
console.log(msg)
});
console.log(`End of Promise.all(..)`)
})
//Result
/*
@AshanthaLahiru
AshanthaLahiru / async_performance1.js
Last active April 29, 2018 11:57
Async performance
let tryAsync = async () => {
let p1 = promise(1000) // |-----|
let p2 = promise(2000) // |-----|-----|
let p3 = promise(3000) // |-----|-----|-----|
console.log(await p1)
console.log(await p2)
console.log(await p3)
}
@AshanthaLahiru
AshanthaLahiru / async_performance2.js
Last active April 29, 2018 11:58
Async performance
let tryAsync = async () => {
console.log(await promise(1000)) // |-----|
console.log(await promise(2000)) // |-----|-----|
console.log(await promise(3000)) // |-----|-----|-----|
}
tryAsync()
//Result
/*
@AshanthaLahiru
AshanthaLahiru / async_performance3.js
Last active April 29, 2018 12:00
Async performance
let tryAsync = async () => {
let msgs = await Promise.all([promise(1000), promise(2000), promise(3000)])
msgs.forEach(msg => {
console.log(msg)
});
console.log(`End of Promise.all(..)`)
}
tryAsync()
//Result
@AshanthaLahiru
AshanthaLahiru / serverless.yml
Created May 3, 2018 01:00
Simple REST API configurations
service: HelloService
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.hello
events:
- http:
path: hello/welcome