1
1
'use strict' ;
2
2
3
- //This file contains then/promise specific extensions to the core promise API
4
-
5
- var Promise = require ( './core.js' )
6
- var asap = require ( 'asap' )
7
-
8
- module . exports = Promise
9
-
10
- /* Static Functions */
11
-
12
- function ValuePromise ( value ) {
13
- this . then = function ( onFulfilled ) {
14
- if ( typeof onFulfilled !== 'function' ) return this
15
- return new Promise ( function ( resolve , reject ) {
16
- asap ( function ( ) {
17
- try {
18
- resolve ( onFulfilled ( value ) )
19
- } catch ( ex ) {
20
- reject ( ex ) ;
21
- }
22
- } )
23
- } )
24
- }
25
- }
26
- ValuePromise . prototype = Promise . prototype
27
-
28
- var TRUE = new ValuePromise ( true )
29
- var FALSE = new ValuePromise ( false )
30
- var NULL = new ValuePromise ( null )
31
- var UNDEFINED = new ValuePromise ( undefined )
32
- var ZERO = new ValuePromise ( 0 )
33
- var EMPTYSTRING = new ValuePromise ( '' )
34
-
35
- Promise . resolve = function ( value ) {
36
- if ( value instanceof Promise ) return value
37
-
38
- if ( value === null ) return NULL
39
- if ( value === undefined ) return UNDEFINED
40
- if ( value === true ) return TRUE
41
- if ( value === false ) return FALSE
42
- if ( value === 0 ) return ZERO
43
- if ( value === '' ) return EMPTYSTRING
44
-
45
- if ( typeof value === 'object' || typeof value === 'function' ) {
46
- try {
47
- var then = value . then
48
- if ( typeof then === 'function' ) {
49
- return new Promise ( then . bind ( value ) )
50
- }
51
- } catch ( ex ) {
52
- return new Promise ( function ( resolve , reject ) {
53
- reject ( ex )
54
- } )
55
- }
56
- }
57
-
58
- return new ValuePromise ( value )
59
- }
60
-
61
- Promise . from = Promise . cast = function ( value ) {
62
- var err = new Error ( 'Promise.from and Promise.cast are deprecated, use Promise.resolve instead' )
63
- err . name = 'Warning'
64
- console . warn ( err . stack )
65
- return Promise . resolve ( value )
66
- }
67
-
68
- Promise . denodeify = function ( fn , argumentCount ) {
69
- argumentCount = argumentCount || Infinity
70
- return function ( ) {
71
- var self = this
72
- var args = Array . prototype . slice . call ( arguments )
73
- return new Promise ( function ( resolve , reject ) {
74
- while ( args . length && args . length > argumentCount ) {
75
- args . pop ( )
76
- }
77
- args . push ( function ( err , res ) {
78
- if ( err ) reject ( err )
79
- else resolve ( res )
80
- } )
81
- fn . apply ( self , args )
82
- } )
83
- }
84
- }
85
- Promise . nodeify = function ( fn ) {
86
- return function ( ) {
87
- var args = Array . prototype . slice . call ( arguments )
88
- var callback = typeof args [ args . length - 1 ] === 'function' ? args . pop ( ) : null
89
- var ctx = this
90
- try {
91
- return fn . apply ( this , arguments ) . nodeify ( callback , ctx )
92
- } catch ( ex ) {
93
- if ( callback === null || typeof callback == 'undefined' ) {
94
- return new Promise ( function ( resolve , reject ) { reject ( ex ) } )
95
- } else {
96
- asap ( function ( ) {
97
- callback . call ( ctx , ex )
98
- } )
99
- }
100
- }
101
- }
102
- }
103
-
104
- Promise . all = function ( arr ) {
105
- if ( arguments . length !== 1 || ! Array . isArray ( arr ) )
106
- return variadicAll . apply ( this , arguments )
107
-
108
- var args = Array . prototype . slice . call ( arr )
109
-
110
- return new Promise ( function ( resolve , reject ) {
111
- if ( args . length === 0 ) return resolve ( [ ] )
112
- var remaining = args . length
113
- function res ( i , val ) {
114
- try {
115
- if ( val && ( typeof val === 'object' || typeof val === 'function' ) ) {
116
- var then = val . then
117
- if ( typeof then === 'function' ) {
118
- then . call ( val , function ( val ) { res ( i , val ) } , reject )
119
- return
120
- }
121
- }
122
- args [ i ] = val
123
- if ( -- remaining === 0 ) {
124
- resolve ( args ) ;
125
- }
126
- } catch ( ex ) {
127
- reject ( ex )
128
- }
129
- }
130
- for ( var i = 0 ; i < args . length ; i ++ ) {
131
- res ( i , args [ i ] )
132
- }
133
- } )
134
- }
135
-
136
- function variadicAll ( ) {
137
- var err = new Error ( 'Promise.all should be called with a single array, calling it with multiple arguments is deprecated' )
138
- err . name = 'Warning'
139
- console . warn ( err . stack )
140
-
141
- return Promise . all ( Array . prototype . slice . call ( arguments ) )
142
- }
143
-
144
- Promise . reject = function ( value ) {
145
- return new Promise ( function ( resolve , reject ) {
146
- reject ( value ) ;
147
- } ) ;
148
- }
149
-
150
- Promise . race = function ( values ) {
151
- return new Promise ( function ( resolve , reject ) {
152
- values . forEach ( function ( value ) {
153
- Promise . resolve ( value ) . then ( resolve , reject ) ;
154
- } )
155
- } ) ;
156
- }
157
-
158
- /* Prototype Methods */
159
-
160
- Promise . prototype . done = function ( onFulfilled , onRejected ) {
161
- var self = arguments . length ? this . then . apply ( this , arguments ) : this
162
- self . then ( null , function ( err ) {
163
- asap ( function ( ) {
164
- throw err
165
- } )
166
- } )
167
- }
168
-
169
- Promise . prototype . nodeify = function ( callback , ctx ) {
170
- if ( typeof callback != 'function' ) return this
171
-
172
- this . then ( function ( value ) {
173
- asap ( function ( ) {
174
- callback . call ( ctx , null , value )
175
- } )
176
- } , function ( err ) {
177
- asap ( function ( ) {
178
- callback . call ( ctx , err )
179
- } )
180
- } )
181
- }
182
-
183
- Promise . prototype [ 'catch' ] = function ( onRejected ) {
184
- return this . then ( null , onRejected ) ;
185
- }
3
+ module . exports = require ( './lib/core.js' )
4
+ require ( './lib/done.js' )
5
+ require ( './lib/es6-extensions.js' )
6
+ require ( './lib/node-extensions.js' )
0 commit comments