Guest User

Untitled

a guest
May 24th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. import AWSXRay from 'aws-xray-sdk-core'
  2.  
  3. AWSXRay.capturePromise()
  4. // Make sure that this function is not called again.
  5. // It would wrap the promise prototype multiple times.
  6. AWSXRay.capturePromise = function () {}
  7.  
  8. export function captureAsyncFunc (name, func) {
  9. return new Promise(function (resolve, reject) {
  10. AWSXRay.captureAsyncFunc(name, segment => {
  11. func(segment).then(
  12. (result) => {
  13. segment.close()
  14. resolve(result)
  15. },
  16. (error) => {
  17. segment.close(error)
  18. reject(error)
  19. }
  20. )
  21. })
  22. })
  23. }
  24.  
  25. // Example usage
  26. const result = await captureAsyncFunc('doSomethingAsync',
  27. (segment) => doSomethingAsync('awesome')
  28. )
  29.  
  30. async function doSomethingAsync (arg) {
  31. // Do something async
  32. return arg
  33. }
Add Comment
Please, Sign In to add comment