Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.57 KB | None | 0 0
  1. setTimeout(()=>{
  2. console.log('final final')
  3. }, 3000)
  4.  
  5. /**
  6. * Effect: a promise is not returned, nor is a callback called
  7. * Findings: the outer setTimeout 'final final' runs to the end
  8. */
  9. exports.emptiness = (event, context, callback) => {
  10. console.log('i do nothing')
  11. }
  12.  
  13. /**
  14. * Effect: old school callback style
  15. * Findings: callback calls after sleep as expected
  16. */
  17. exports.nonAsyncCallback = (event, context, callback) => {
  18. console.time('nonAsyncCallback')
  19. sleep(2)
  20. .then(()=>{
  21. console.timeEnd('nonAsyncCallback')
  22. callback(null, 'nonAsyncCallbackSuccess')
  23. })
  24. };
  25.  
  26. /**
  27. * Effect: newschool promise style
  28. * Findings: promise resolves after sleep as expected
  29. */
  30. exports.asyncStyle = async (event) => {
  31. console.time('asyncStyle')
  32. return sleep(2)
  33. .then(()=>{
  34. console.timeEnd('asyncStyle')
  35. })
  36. }
  37.  
  38. /**
  39. * Effect: async function + use of callback
  40. * Findings: first one called back/resolved wins, outer 'final final' setTimeout never finishes
  41. * should be fine if they both return the same thing,
  42. * could be dodgy if callback has one thing and promise has another
  43. */
  44. exports.asyncCallbackMadness = async (event, context, callback) => {
  45. console.time('asyncCallbackMadness')
  46. await sleep(2)
  47.  
  48.  
  49. console.timeEnd('asyncCallbackMadness')
  50. setTimeout(()=>{
  51. callback(null, 'asyncCallbackMadnessCallback')
  52.  
  53. }, 0)
  54. return Promise.resolve('asyncCallbackMadnessPromise')
  55. }
  56.  
  57. //shared helper
  58. async function sleep(seconds) {
  59. return new Promise(resolve => setTimeout(resolve, seconds * 1000))
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement