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