Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unwrapSync.js 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. 'use strict'
  2. const ExtendableError = require('es6-error')
  3. const constants = require('./constants')
  4. class UnwrapError extends ExtendableError {
  5. constructor (thenable) {
  6. super('Could not unwrap asynchronous thenable')
  7. this.thenable = thenable
  8. }
  9. }
  10. // Logic is derived from the Promise Resolution Procedure, as described in the
  11. // Promises/A+ specification: https://promisesaplus.com/#point-45
  12. //
  13. // Note that there is no cycle detection.
  14. function unwrapSync (x) {
  15. if (!x || typeof x !== 'object' && typeof x !== 'function') {
  16. return x
  17. }
  18. const then = x.then
  19. if (typeof then !== 'function') return x
  20. let state = constants.PENDING
  21. let value
  22. const unwrapValue = y => {
  23. if (state === constants.PENDING) {
  24. state = constants.RESOLVED
  25. value = y
  26. }
  27. }
  28. const unwrapReason = r => {
  29. if (state === constants.PENDING) {
  30. state = constants.REJECTED
  31. value = r
  32. }
  33. }
  34. then.call(x, unwrapValue, unwrapReason)
  35. if (state === constants.PENDING) {
  36. state = constants.ASYNC
  37. throw new UnwrapError(x)
  38. }
  39. if (state === constants.RESOLVED) {
  40. return unwrapSync(value)
  41. }
  42. // state === REJECTED
  43. throw value
  44. }
  45. module.exports = unwrapSync