Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- setTimeout(()=>{
- console.log('final final')
- }, 3000)
- /**
- * Effect: a promise is not returned, nor is a callback called
- * Findings: the outer setTimeout 'final final' runs to the end
- */
- exports.emptiness = (event, context, callback) => {
- console.log('i do nothing')
- }
- /**
- * Effect: old school callback style
- * Findings: callback calls after sleep as expected
- */
- exports.nonAsyncCallback = (event, context, callback) => {
- console.time('nonAsyncCallback')
- sleep(2)
- .then(()=>{
- console.timeEnd('nonAsyncCallback')
- callback(null, 'nonAsyncCallbackSuccess')
- })
- };
- /**
- * Effect: newschool promise style
- * Findings: promise resolves after sleep as expected
- */
- exports.asyncStyle = async (event) => {
- console.time('asyncStyle')
- return sleep(2)
- .then(()=>{
- console.timeEnd('asyncStyle')
- })
- }
- /**
- * Effect: async function + use of callback
- * Findings: first one called back/resolved wins, outer 'final final' setTimeout never finishes
- * should be fine if they both return the same thing,
- * could be dodgy if callback has one thing and promise has another
- */
- exports.asyncCallbackMadness = async (event, context, callback) => {
- console.time('asyncCallbackMadness')
- await sleep(2)
- console.timeEnd('asyncCallbackMadness')
- setTimeout(()=>{
- callback(null, 'asyncCallbackMadnessCallback')
- }, 0)
- return Promise.resolve('asyncCallbackMadnessPromise')
- }
- //shared helper
- async function sleep(seconds) {
- return new Promise(resolve => setTimeout(resolve, seconds * 1000))
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement