@@ -89,9 +89,9 @@ def fulfill(self, x):
89
89
90
90
if self is x :
91
91
raise TypeError ("Cannot resolve promise with itself." )
92
- elif _isPromise (x ):
92
+ elif is_thenable (x ):
93
93
try :
94
- _promisify (x ).done (self .fulfill , self .reject )
94
+ promisify (x ).done (self .fulfill , self .reject )
95
95
except Exception as e :
96
96
self .reject (e )
97
97
else :
@@ -159,17 +159,17 @@ def reject(self, reason):
159
159
pass
160
160
161
161
@property
162
- def isPending (self ):
162
+ def is_pending (self ):
163
163
"""Indicate whether the Promise is still pending. Could be wrong the moment the function returns."""
164
164
return self ._state == self .PENDING
165
165
166
166
@property
167
- def isFulfilled (self ):
167
+ def is_fulfilled (self ):
168
168
"""Indicate whether the Promise has been fulfilled. Could be wrong the moment the function returns."""
169
169
return self ._state == self .FULFILLED
170
170
171
171
@property
172
- def isRejected (self ):
172
+ def is_rejected (self ):
173
173
"""Indicate whether the Promise has been rejected. Could be wrong the moment the function returns."""
174
174
return self ._state == self .REJECTED
175
175
@@ -200,13 +200,13 @@ def wait(self, timeout=None):
200
200
"""
201
201
self ._event .wait (timeout )
202
202
203
- def addCallback (self , f ):
203
+ def add_callback (self , f ):
204
204
"""
205
205
Add a callback for when this promis is fulfilled. Note that
206
206
if you intend to use the value of the promise somehow in
207
207
the callback, it is more convenient to use the 'then' method.
208
208
"""
209
- assert _isFunction (f )
209
+ assert _is_function (f )
210
210
211
211
with self ._cb_lock :
212
212
if self ._state == self .PENDING :
@@ -221,14 +221,14 @@ def addCallback(self, f):
221
221
else :
222
222
pass
223
223
224
- def addErrback (self , f ):
224
+ def add_errback (self , f ):
225
225
"""
226
226
Add a callback for when this promis is rejected. Note that
227
227
if you intend to use the rejection reason of the promise
228
228
somehow in the callback, it is more convenient to use
229
229
the 'then' method.
230
230
"""
231
- assert _isFunction (f )
231
+ assert _is_function (f )
232
232
233
233
with self ._cb_lock :
234
234
if self ._state == self .PENDING :
@@ -256,9 +256,9 @@ def done(self, success=None, failure=None):
256
256
"""
257
257
with self ._cb_lock :
258
258
if success is not None :
259
- self .addCallback (success )
259
+ self .add_callback (success )
260
260
if failure is not None :
261
- self .addErrback (failure )
261
+ self .add_errback (failure )
262
262
263
263
def done_all (self , * handlers ):
264
264
"""
@@ -311,33 +311,33 @@ def then(self, success=None, failure=None):
311
311
"""
312
312
ret = Promise ()
313
313
314
- def callAndFulfill (v ):
314
+ def call_and_fulfill (v ):
315
315
"""
316
316
A callback to be invoked if the "self promise"
317
317
is fulfilled.
318
318
"""
319
319
try :
320
- if _isFunction (success ):
320
+ if _is_function (success ):
321
321
ret .fulfill (success (v ))
322
322
else :
323
323
ret .fulfill (v )
324
324
except Exception as e :
325
325
ret .reject (e )
326
326
327
- def callAndReject (r ):
327
+ def call_and_reject (r ):
328
328
"""
329
329
A callback to be invoked if the "self promise"
330
330
is rejected.
331
331
"""
332
332
try :
333
- if _isFunction (failure ):
333
+ if _is_function (failure ):
334
334
ret .fulfill (failure (r ))
335
335
else :
336
336
ret .reject (r )
337
337
except Exception as e :
338
338
ret .reject (e )
339
339
340
- self .done (callAndFulfill , callAndReject )
340
+ self .done (call_and_fulfill , call_and_reject )
341
341
342
342
return ret
343
343
@@ -374,74 +374,65 @@ def then_all(self, *handlers):
374
374
375
375
@staticmethod
376
376
def all (values_or_promises ):
377
- return listPromise (values_or_promises )
377
+ """
378
+ A special function that takes a bunch of promises
379
+ and turns them into a promise for a vector of values.
380
+ In other words, this turns an list of promises for values
381
+ into a promise for a list of values.
382
+ """
383
+ promises = list (filter (is_thenable , values_or_promises ))
384
+ if len (promises ) == 0 :
385
+ # All the values or promises are resolved
386
+ return Promise .fulfilled (values_or_promises )
387
+
388
+ all_promise = Promise ()
389
+ counter = CountdownLatch (len (promises ))
390
+
391
+ def handleSuccess (_ ):
392
+ if counter .dec () == 0 :
393
+ values = list (map (lambda p : p .value if p in promises else p , values_or_promises ))
394
+ all_promise .fulfill (values )
395
+
396
+ for p in promises :
397
+ promisify (p ).done (handleSuccess , all_promise .reject )
398
+
399
+ return all_promise
378
400
379
401
380
- def _isFunction (v ):
402
+ def _is_function (v ):
381
403
"""
382
404
A utility function to determine if the specified
383
405
value is a function.
384
406
"""
385
407
return v is not None and hasattr (v , "__call__" )
386
408
387
409
388
- def _isPromise (obj ):
410
+ def is_thenable (obj ):
389
411
"""
390
412
A utility function to determine if the specified
391
413
object is a promise using "duck typing".
392
414
"""
393
415
return isinstance (obj , Promise ) or (
394
- hasattr (obj , "done" ) and _isFunction (getattr (obj , "done" ))) or (
395
- hasattr (obj , "then" ) and _isFunction (getattr (obj , "then" )))
416
+ hasattr (obj , "done" ) and _is_function (getattr (obj , "done" ))) or (
417
+ hasattr (obj , "then" ) and _is_function (getattr (obj , "then" )))
396
418
397
419
398
- is_thenable = _isPromise
399
-
400
-
401
- def _promisify (obj ):
420
+ def promisify (obj ):
402
421
if isinstance (obj , Promise ):
403
422
return obj
404
- elif hasattr (obj , "done" ) and _isFunction (getattr (obj , "done" )):
423
+ elif hasattr (obj , "done" ) and _is_function (getattr (obj , "done" )):
405
424
p = Promise ()
406
425
obj .done (p .fulfill , p .reject )
407
426
return p
408
- elif hasattr (obj , "then" ) and _isFunction (getattr (obj , "then" )):
427
+ elif hasattr (obj , "then" ) and _is_function (getattr (obj , "then" )):
409
428
p = Promise ()
410
429
obj .then (p .fulfill , p .reject )
411
430
return p
412
431
else :
413
432
raise TypeError ("Object is not a Promise like object." )
414
433
415
- promisify = _promisify
416
-
417
434
418
- def listPromise (values_or_promises ):
419
- """
420
- A special function that takes a bunch of promises
421
- and turns them into a promise for a vector of values.
422
- In other words, this turns an list of promises for values
423
- into a promise for a list of values.
424
- """
425
- promises = list (filter (_isPromise , values_or_promises ))
426
- if len (promises ) == 0 :
427
- # All the values or promises are resolved
428
- return Promise .fulfilled (values_or_promises )
429
-
430
- ret = Promise ()
431
- counter = CountdownLatch (len (promises ))
432
-
433
- def handleSuccess (_ ):
434
- if counter .dec () == 0 :
435
- values = list (map (lambda p : p .value if p in promises else p , values_or_promises ))
436
- ret .fulfill (values )
437
-
438
- for p in promises :
439
- _promisify (p ).done (handleSuccess , ret .reject )
440
-
441
- return ret
442
-
443
-
444
- def dictPromise (m ):
435
+ def promise_for_dict (m ):
445
436
"""
446
437
A special function that takes a dictionary of promises
447
438
and turns them into a promise for a dictionary of values.
@@ -458,14 +449,3 @@ def handleSuccess(resolved_values):
458
449
return dict_type (zip (keys , resolved_values ))
459
450
460
451
return Promise .all (values ).then (handleSuccess )
461
-
462
-
463
- promise_for_dict = dictPromise
464
-
465
-
466
- def _process (p , f ):
467
- try :
468
- val = f ()
469
- p .fulfill (val )
470
- except Exception as e :
471
- p .reject (e )
0 commit comments