@zeppos/zeus-cli 1.3.5 → 1.3.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/bin/main.js +8 -8
  2. package/modules/build.js +5 -3
  3. package/modules/create.js +6 -5
  4. package/package.json +1 -1
  5. package/private-modules/zeppos-app-utils/dist/config/device.js +105 -45
  6. package/private-modules/zeppos-app-utils/dist/modules/create/local-app.js +3 -2
  7. package/private-modules/zeppos-app-utils/dist/modules/fetchDevices.js +87 -0
  8. package/private-modules/zeppos-app-utils/dist/modules/index.js +1 -0
  9. package/private-modules/zeppos-app-utils/dist/public/template/os1.0/app/fetch-api/app-side/index.js +6 -18
  10. package/private-modules/zeppos-app-utils/dist/public/template/os1.0/app/fetch-api/pages/index.js +6 -6
  11. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/app-side/index.js +6 -18
  12. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/pages/index.js +6 -6
  13. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.b/icon.png +0 -0
  14. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.r/icon.png +0 -0
  15. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/empty/assets/default.s/icon.png +0 -0
  16. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/app-side/index.js +7 -35
  17. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.js +1 -1
  18. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.r.layout.js +8 -8
  19. package/private-modules/zeppos-app-utils/dist/public/template/os3.0/app/fetch-api/page/index.s.layout.js +1 -1
  20. package/private-modules/zeppos-app-utils/dist/tools/index.js +2 -1
  21. package/private-modules/zeppos-app-utils/dist/config/device-server.js +0 -116
  22. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/.prettierrc.js +0 -6
  23. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app-side/index.js +0 -45
  24. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app.js +0 -25
  25. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/app.json +0 -64
  26. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/assets/gts/icon.png +0 -0
  27. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.page.js +0 -155
  28. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.page.json +0 -1
  29. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/home/index.style.js +0 -92
  30. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/page/i18n/en-US.po +0 -9
  31. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/secondary-widget/index.js +0 -160
  32. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/setting/i18n/en-US.po +0 -5
  33. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/setting/index.js +0 -143
  34. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/data.js +0 -67
  35. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/defer.js +0 -35
  36. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/device-polyfill.js +0 -6
  37. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/es6-promise.js +0 -1178
  38. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/event.js +0 -42
  39. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/message-side.js +0 -1190
  40. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/shared/message.js +0 -1196
  41. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/utils/constants.js +0 -1
  42. package/private-modules/zeppos-app-utils/dist/public/template/os2.0/app/fetch-api/todo-list/utils/index.js +0 -38
@@ -1,1178 +0,0 @@
1
- /*!
2
- * @overview es6-promise - a tiny implementation of Promises/A+.
3
- * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
4
- * @license Licensed under MIT license
5
- * See https://raw.githubusercontent.com/stefanpenner/es6-promise/master/LICENSE
6
- * @version v4.2.8+1e68dce6
7
- */
8
-
9
- ;(function (global, factory) {
10
- global.ES6Promise = factory()
11
- })(globalThis, function () {
12
- 'use strict'
13
-
14
- function objectOrFunction(x) {
15
- var type = typeof x
16
- return x !== null && (type === 'object' || type === 'function')
17
- }
18
-
19
- function isFunction(x) {
20
- return typeof x === 'function'
21
- }
22
-
23
- var _isArray = void 0
24
- if (Array.isArray) {
25
- _isArray = Array.isArray
26
- } else {
27
- _isArray = function (x) {
28
- return Object.prototype.toString.call(x) === '[object Array]'
29
- }
30
- }
31
-
32
- var isArray = _isArray
33
-
34
- var len = 0
35
- var vertxNext = void 0
36
- var customSchedulerFn = void 0
37
-
38
- var asap = function asap(callback, arg) {
39
- queue[len] = callback
40
- queue[len + 1] = arg
41
- len += 2
42
- if (len === 2) {
43
- // If len is 2, that means that we need to schedule an async flush.
44
- // If additional callbacks are queued before the queue is flushed, they
45
- // will be processed by this flush that we are scheduling.
46
- if (customSchedulerFn) {
47
- customSchedulerFn(flush)
48
- } else {
49
- scheduleFlush()
50
- }
51
- }
52
- }
53
-
54
- function setScheduler(scheduleFn) {
55
- customSchedulerFn = scheduleFn
56
- }
57
-
58
- function setAsap(asapFn) {
59
- asap = asapFn
60
- }
61
-
62
- var browserWindow = typeof window !== 'undefined' ? window : undefined
63
- var browserGlobal = browserWindow || {}
64
- var BrowserMutationObserver =
65
- browserGlobal.MutationObserver || browserGlobal.WebKitMutationObserver
66
- var isNode =
67
- typeof self === 'undefined' &&
68
- typeof process !== 'undefined' &&
69
- {}.toString.call(process) === '[object process]'
70
-
71
- // test for web worker but not in IE10
72
- var isWorker =
73
- typeof Uint8ClampedArray !== 'undefined' &&
74
- typeof importScripts !== 'undefined' &&
75
- typeof MessageChannel !== 'undefined'
76
-
77
- // node
78
- function useNextTick() {
79
- // node version 0.10.x displays a deprecation warning when nextTick is used recursively
80
- // see https://github.com/cujojs/when/issues/410 for details
81
- return function () {
82
- return process.nextTick(flush)
83
- }
84
- }
85
-
86
- // vertx
87
- function useVertxTimer() {
88
- if (typeof vertxNext !== 'undefined') {
89
- return function () {
90
- vertxNext(flush)
91
- }
92
- }
93
-
94
- return useSetTimeout()
95
- }
96
-
97
- function useMutationObserver() {
98
- var iterations = 0
99
- var observer = new BrowserMutationObserver(flush)
100
- var node = document.createTextNode('')
101
- observer.observe(node, { characterData: true })
102
-
103
- return function () {
104
- node.data = iterations = ++iterations % 2
105
- }
106
- }
107
-
108
- // web worker
109
- function useMessageChannel() {
110
- var channel = new MessageChannel()
111
- channel.port1.onmessage = flush
112
- return function () {
113
- return channel.port2.postMessage(0)
114
- }
115
- }
116
-
117
- function useSetTimeout() {
118
- // Store setTimeout reference so es6-promise will be unaffected by
119
- // other code modifying setTimeout (like sinon.useFakeTimers())
120
- var globalSetTimeout = setTimeout
121
- return function () {
122
- return globalSetTimeout(flush, 1)
123
- }
124
- }
125
-
126
- var queue = new Array(1000)
127
- function flush() {
128
- for (var i = 0; i < len; i += 2) {
129
- var callback = queue[i]
130
- var arg = queue[i + 1]
131
-
132
- callback(arg)
133
-
134
- queue[i] = undefined
135
- queue[i + 1] = undefined
136
- }
137
-
138
- len = 0
139
- }
140
-
141
- function attemptVertx() {
142
- try {
143
- var vertx = Function('return this')().require('vertx')
144
- vertxNext = vertx.runOnLoop || vertx.runOnContext
145
- return useVertxTimer()
146
- } catch (e) {
147
- return useSetTimeout()
148
- }
149
- }
150
-
151
- var scheduleFlush = void 0
152
- // Decide what async method to use to triggering processing of queued callbacks:
153
- if (isNode) {
154
- scheduleFlush = useNextTick()
155
- } else if (BrowserMutationObserver) {
156
- scheduleFlush = useMutationObserver()
157
- } else if (isWorker) {
158
- scheduleFlush = useMessageChannel()
159
- } else if (browserWindow === undefined && typeof require === 'function') {
160
- scheduleFlush = attemptVertx()
161
- } else {
162
- scheduleFlush = useSetTimeout()
163
- }
164
-
165
- function then(onFulfillment, onRejection) {
166
- var parent = this
167
-
168
- var child = new this.constructor(noop)
169
-
170
- if (child[PROMISE_ID] === undefined) {
171
- makePromise(child)
172
- }
173
-
174
- var _state = parent._state
175
-
176
- if (_state) {
177
- var callback = arguments[_state - 1]
178
- asap(function () {
179
- return invokeCallback(_state, child, callback, parent._result)
180
- })
181
- } else {
182
- subscribe(parent, child, onFulfillment, onRejection)
183
- }
184
-
185
- return child
186
- }
187
-
188
- /**
189
- `Promise.resolve` returns a promise that will become resolved with the
190
- passed `value`. It is shorthand for the following:
191
-
192
- ```javascript
193
- let promise = new Promise(function(resolve, reject){
194
- resolve(1);
195
- });
196
-
197
- promise.then(function(value){
198
- // value === 1
199
- });
200
- ```
201
-
202
- Instead of writing the above, your code now simply becomes the following:
203
-
204
- ```javascript
205
- let promise = Promise.resolve(1);
206
-
207
- promise.then(function(value){
208
- // value === 1
209
- });
210
- ```
211
-
212
- @method resolve
213
- @static
214
- @param {Any} value value that the returned promise will be resolved with
215
- Useful for tooling.
216
- @return {Promise} a promise that will become fulfilled with the given
217
- `value`
218
- */
219
- function resolve$1(object) {
220
- /*jshint validthis:true */
221
- var Constructor = this
222
-
223
- if (object && typeof object === 'object' && object.constructor === Constructor) {
224
- return object
225
- }
226
-
227
- var promise = new Constructor(noop)
228
- resolve(promise, object)
229
- return promise
230
- }
231
-
232
- var PROMISE_ID = Math.random().toString(36).substring(2)
233
-
234
- function noop() {}
235
-
236
- var PENDING = void 0
237
- var FULFILLED = 1
238
- var REJECTED = 2
239
-
240
- function selfFulfillment() {
241
- return new TypeError('You cannot resolve a promise with itself')
242
- }
243
-
244
- function cannotReturnOwn() {
245
- return new TypeError('A promises callback cannot return that same promise.')
246
- }
247
-
248
- function tryThen(then$$1, value, fulfillmentHandler, rejectionHandler) {
249
- try {
250
- then$$1.call(value, fulfillmentHandler, rejectionHandler)
251
- } catch (e) {
252
- return e
253
- }
254
- }
255
-
256
- function handleForeignThenable(promise, thenable, then$$1) {
257
- asap(function (promise) {
258
- var sealed = false
259
- var error = tryThen(
260
- then$$1,
261
- thenable,
262
- function (value) {
263
- if (sealed) {
264
- return
265
- }
266
- sealed = true
267
- if (thenable !== value) {
268
- resolve(promise, value)
269
- } else {
270
- fulfill(promise, value)
271
- }
272
- },
273
- function (reason) {
274
- if (sealed) {
275
- return
276
- }
277
- sealed = true
278
-
279
- reject(promise, reason)
280
- },
281
- 'Settle: ' + (promise._label || ' unknown promise')
282
- )
283
-
284
- if (!sealed && error) {
285
- sealed = true
286
- reject(promise, error)
287
- }
288
- }, promise)
289
- }
290
-
291
- function handleOwnThenable(promise, thenable) {
292
- if (thenable._state === FULFILLED) {
293
- fulfill(promise, thenable._result)
294
- } else if (thenable._state === REJECTED) {
295
- reject(promise, thenable._result)
296
- } else {
297
- subscribe(
298
- thenable,
299
- undefined,
300
- function (value) {
301
- return resolve(promise, value)
302
- },
303
- function (reason) {
304
- return reject(promise, reason)
305
- }
306
- )
307
- }
308
- }
309
-
310
- function handleMaybeThenable(promise, maybeThenable, then$$1) {
311
- if (
312
- maybeThenable.constructor === promise.constructor &&
313
- then$$1 === then &&
314
- maybeThenable.constructor.resolve === resolve$1
315
- ) {
316
- handleOwnThenable(promise, maybeThenable)
317
- } else {
318
- if (then$$1 === undefined) {
319
- fulfill(promise, maybeThenable)
320
- } else if (isFunction(then$$1)) {
321
- handleForeignThenable(promise, maybeThenable, then$$1)
322
- } else {
323
- fulfill(promise, maybeThenable)
324
- }
325
- }
326
- }
327
-
328
- function resolve(promise, value) {
329
- if (promise === value) {
330
- reject(promise, selfFulfillment())
331
- } else if (objectOrFunction(value)) {
332
- var then$$1 = void 0
333
- try {
334
- then$$1 = value.then
335
- } catch (error) {
336
- reject(promise, error)
337
- return
338
- }
339
- handleMaybeThenable(promise, value, then$$1)
340
- } else {
341
- fulfill(promise, value)
342
- }
343
- }
344
-
345
- function publishRejection(promise) {
346
- if (promise._onerror) {
347
- promise._onerror(promise._result)
348
- }
349
-
350
- publish(promise)
351
- }
352
-
353
- function fulfill(promise, value) {
354
- if (promise._state !== PENDING) {
355
- return
356
- }
357
-
358
- promise._result = value
359
- promise._state = FULFILLED
360
-
361
- if (promise._subscribers.length !== 0) {
362
- asap(publish, promise)
363
- }
364
- }
365
-
366
- function reject(promise, reason) {
367
- if (promise._state !== PENDING) {
368
- return
369
- }
370
- promise._state = REJECTED
371
- promise._result = reason
372
-
373
- asap(publishRejection, promise)
374
- }
375
-
376
- function subscribe(parent, child, onFulfillment, onRejection) {
377
- var _subscribers = parent._subscribers
378
- var length = _subscribers.length
379
-
380
- parent._onerror = null
381
-
382
- _subscribers[length] = child
383
- _subscribers[length + FULFILLED] = onFulfillment
384
- _subscribers[length + REJECTED] = onRejection
385
-
386
- if (length === 0 && parent._state) {
387
- asap(publish, parent)
388
- }
389
- }
390
-
391
- function publish(promise) {
392
- var subscribers = promise._subscribers
393
- var settled = promise._state
394
-
395
- if (subscribers.length === 0) {
396
- return
397
- }
398
-
399
- var child = void 0,
400
- callback = void 0,
401
- detail = promise._result
402
-
403
- for (var i = 0; i < subscribers.length; i += 3) {
404
- child = subscribers[i]
405
- callback = subscribers[i + settled]
406
-
407
- if (child) {
408
- invokeCallback(settled, child, callback, detail)
409
- } else {
410
- callback(detail)
411
- }
412
- }
413
-
414
- promise._subscribers.length = 0
415
- }
416
-
417
- function invokeCallback(settled, promise, callback, detail) {
418
- var hasCallback = isFunction(callback),
419
- value = void 0,
420
- error = void 0,
421
- succeeded = true
422
-
423
- if (hasCallback) {
424
- try {
425
- value = callback(detail)
426
- } catch (e) {
427
- succeeded = false
428
- error = e
429
- }
430
-
431
- if (promise === value) {
432
- reject(promise, cannotReturnOwn())
433
- return
434
- }
435
- } else {
436
- value = detail
437
- }
438
-
439
- if (promise._state !== PENDING) {
440
- // noop
441
- } else if (hasCallback && succeeded) {
442
- resolve(promise, value)
443
- } else if (succeeded === false) {
444
- reject(promise, error)
445
- } else if (settled === FULFILLED) {
446
- fulfill(promise, value)
447
- } else if (settled === REJECTED) {
448
- reject(promise, value)
449
- }
450
- }
451
-
452
- function initializePromise(promise, resolver) {
453
- try {
454
- resolver(
455
- function resolvePromise(value) {
456
- resolve(promise, value)
457
- },
458
- function rejectPromise(reason) {
459
- reject(promise, reason)
460
- }
461
- )
462
- } catch (e) {
463
- reject(promise, e)
464
- }
465
- }
466
-
467
- var id = 0
468
- function nextId() {
469
- return id++
470
- }
471
-
472
- function makePromise(promise) {
473
- promise[PROMISE_ID] = id++
474
- promise._state = undefined
475
- promise._result = undefined
476
- promise._subscribers = []
477
- }
478
-
479
- function validationError() {
480
- return new Error('Array Methods must be provided an Array')
481
- }
482
-
483
- var Enumerator = (function () {
484
- function Enumerator(Constructor, input) {
485
- this._instanceConstructor = Constructor
486
- this.promise = new Constructor(noop)
487
-
488
- if (!this.promise[PROMISE_ID]) {
489
- makePromise(this.promise)
490
- }
491
-
492
- if (isArray(input)) {
493
- this.length = input.length
494
- this._remaining = input.length
495
-
496
- this._result = new Array(this.length)
497
-
498
- if (this.length === 0) {
499
- fulfill(this.promise, this._result)
500
- } else {
501
- this.length = this.length || 0
502
- this._enumerate(input)
503
- if (this._remaining === 0) {
504
- fulfill(this.promise, this._result)
505
- }
506
- }
507
- } else {
508
- reject(this.promise, validationError())
509
- }
510
- }
511
-
512
- Enumerator.prototype._enumerate = function _enumerate(input) {
513
- for (var i = 0; this._state === PENDING && i < input.length; i++) {
514
- this._eachEntry(input[i], i)
515
- }
516
- }
517
-
518
- Enumerator.prototype._eachEntry = function _eachEntry(entry, i) {
519
- var c = this._instanceConstructor
520
- var resolve$$1 = c.resolve
521
-
522
- if (resolve$$1 === resolve$1) {
523
- var _then = void 0
524
- var error = void 0
525
- var didError = false
526
- try {
527
- _then = entry.then
528
- } catch (e) {
529
- didError = true
530
- error = e
531
- }
532
-
533
- if (_then === then && entry._state !== PENDING) {
534
- this._settledAt(entry._state, i, entry._result)
535
- } else if (typeof _then !== 'function') {
536
- this._remaining--
537
- this._result[i] = entry
538
- } else if (c === Promise$1) {
539
- var promise = new c(noop)
540
- if (didError) {
541
- reject(promise, error)
542
- } else {
543
- handleMaybeThenable(promise, entry, _then)
544
- }
545
- this._willSettleAt(promise, i)
546
- } else {
547
- this._willSettleAt(
548
- new c(function (resolve$$1) {
549
- return resolve$$1(entry)
550
- }),
551
- i
552
- )
553
- }
554
- } else {
555
- this._willSettleAt(resolve$$1(entry), i)
556
- }
557
- }
558
-
559
- Enumerator.prototype._settledAt = function _settledAt(state, i, value) {
560
- var promise = this.promise
561
-
562
- if (promise._state === PENDING) {
563
- this._remaining--
564
-
565
- if (state === REJECTED) {
566
- reject(promise, value)
567
- } else {
568
- this._result[i] = value
569
- }
570
- }
571
-
572
- if (this._remaining === 0) {
573
- fulfill(promise, this._result)
574
- }
575
- }
576
-
577
- Enumerator.prototype._willSettleAt = function _willSettleAt(promise, i) {
578
- var enumerator = this
579
-
580
- subscribe(
581
- promise,
582
- undefined,
583
- function (value) {
584
- return enumerator._settledAt(FULFILLED, i, value)
585
- },
586
- function (reason) {
587
- return enumerator._settledAt(REJECTED, i, reason)
588
- }
589
- )
590
- }
591
-
592
- return Enumerator
593
- })()
594
-
595
- /**
596
- `Promise.all` accepts an array of promises, and returns a new promise which
597
- is fulfilled with an array of fulfillment values for the passed promises, or
598
- rejected with the reason of the first passed promise to be rejected. It casts all
599
- elements of the passed iterable to promises as it runs this algorithm.
600
-
601
- Example:
602
-
603
- ```javascript
604
- let promise1 = resolve(1);
605
- let promise2 = resolve(2);
606
- let promise3 = resolve(3);
607
- let promises = [ promise1, promise2, promise3 ];
608
-
609
- Promise.all(promises).then(function(array){
610
- // The array here would be [ 1, 2, 3 ];
611
- });
612
- ```
613
-
614
- If any of the `promises` given to `all` are rejected, the first promise
615
- that is rejected will be given as an argument to the returned promises's
616
- rejection handler. For example:
617
-
618
- Example:
619
-
620
- ```javascript
621
- let promise1 = resolve(1);
622
- let promise2 = reject(new Error("2"));
623
- let promise3 = reject(new Error("3"));
624
- let promises = [ promise1, promise2, promise3 ];
625
-
626
- Promise.all(promises).then(function(array){
627
- // Code here never runs because there are rejected promises!
628
- }, function(error) {
629
- // error.message === "2"
630
- });
631
- ```
632
-
633
- @method all
634
- @static
635
- @param {Array} entries array of promises
636
- @param {String} label optional string for labeling the promise.
637
- Useful for tooling.
638
- @return {Promise} promise that is fulfilled when all `promises` have been
639
- fulfilled, or rejected if any of them become rejected.
640
- @static
641
- */
642
- function all(entries) {
643
- return new Enumerator(this, entries).promise
644
- }
645
-
646
- /**
647
- `Promise.race` returns a new promise which is settled in the same way as the
648
- first passed promise to settle.
649
-
650
- Example:
651
-
652
- ```javascript
653
- let promise1 = new Promise(function(resolve, reject){
654
- setTimeout(function(){
655
- resolve('promise 1');
656
- }, 200);
657
- });
658
-
659
- let promise2 = new Promise(function(resolve, reject){
660
- setTimeout(function(){
661
- resolve('promise 2');
662
- }, 100);
663
- });
664
-
665
- Promise.race([promise1, promise2]).then(function(result){
666
- // result === 'promise 2' because it was resolved before promise1
667
- // was resolved.
668
- });
669
- ```
670
-
671
- `Promise.race` is deterministic in that only the state of the first
672
- settled promise matters. For example, even if other promises given to the
673
- `promises` array argument are resolved, but the first settled promise has
674
- become rejected before the other promises became fulfilled, the returned
675
- promise will become rejected:
676
-
677
- ```javascript
678
- let promise1 = new Promise(function(resolve, reject){
679
- setTimeout(function(){
680
- resolve('promise 1');
681
- }, 200);
682
- });
683
-
684
- let promise2 = new Promise(function(resolve, reject){
685
- setTimeout(function(){
686
- reject(new Error('promise 2'));
687
- }, 100);
688
- });
689
-
690
- Promise.race([promise1, promise2]).then(function(result){
691
- // Code here never runs
692
- }, function(reason){
693
- // reason.message === 'promise 2' because promise 2 became rejected before
694
- // promise 1 became fulfilled
695
- });
696
- ```
697
-
698
- An example real-world use case is implementing timeouts:
699
-
700
- ```javascript
701
- Promise.race([ajax('foo.json'), timeout(5000)])
702
- ```
703
-
704
- @method race
705
- @static
706
- @param {Array} promises array of promises to observe
707
- Useful for tooling.
708
- @return {Promise} a promise which settles in the same way as the first passed
709
- promise to settle.
710
- */
711
- function race(entries) {
712
- /*jshint validthis:true */
713
- var Constructor = this
714
-
715
- if (!isArray(entries)) {
716
- return new Constructor(function (_, reject) {
717
- return reject(new TypeError('You must pass an array to race.'))
718
- })
719
- } else {
720
- return new Constructor(function (resolve, reject) {
721
- var length = entries.length
722
- for (var i = 0; i < length; i++) {
723
- Constructor.resolve(entries[i]).then(resolve, reject)
724
- }
725
- })
726
- }
727
- }
728
-
729
- /**
730
- `Promise.reject` returns a promise rejected with the passed `reason`.
731
- It is shorthand for the following:
732
-
733
- ```javascript
734
- let promise = new Promise(function(resolve, reject){
735
- reject(new Error('WHOOPS'));
736
- });
737
-
738
- promise.then(function(value){
739
- // Code here doesn't run because the promise is rejected!
740
- }, function(reason){
741
- // reason.message === 'WHOOPS'
742
- });
743
- ```
744
-
745
- Instead of writing the above, your code now simply becomes the following:
746
-
747
- ```javascript
748
- let promise = Promise.reject(new Error('WHOOPS'));
749
-
750
- promise.then(function(value){
751
- // Code here doesn't run because the promise is rejected!
752
- }, function(reason){
753
- // reason.message === 'WHOOPS'
754
- });
755
- ```
756
-
757
- @method reject
758
- @static
759
- @param {Any} reason value that the returned promise will be rejected with.
760
- Useful for tooling.
761
- @return {Promise} a promise rejected with the given `reason`.
762
- */
763
- function reject$1(reason) {
764
- /*jshint validthis:true */
765
- var Constructor = this
766
- var promise = new Constructor(noop)
767
- reject(promise, reason)
768
- return promise
769
- }
770
-
771
- function needsResolver() {
772
- throw new TypeError(
773
- 'You must pass a resolver function as the first argument to the promise constructor'
774
- )
775
- }
776
-
777
- function needsNew() {
778
- throw new TypeError(
779
- "Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function."
780
- )
781
- }
782
-
783
- /**
784
- Promise objects represent the eventual result of an asynchronous operation. The
785
- primary way of interacting with a promise is through its `then` method, which
786
- registers callbacks to receive either a promise's eventual value or the reason
787
- why the promise cannot be fulfilled.
788
-
789
- Terminology
790
- -----------
791
-
792
- - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
793
- - `thenable` is an object or function that defines a `then` method.
794
- - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
795
- - `exception` is a value that is thrown using the throw statement.
796
- - `reason` is a value that indicates why a promise was rejected.
797
- - `settled` the final resting state of a promise, fulfilled or rejected.
798
-
799
- A promise can be in one of three states: pending, fulfilled, or rejected.
800
-
801
- Promises that are fulfilled have a fulfillment value and are in the fulfilled
802
- state. Promises that are rejected have a rejection reason and are in the
803
- rejected state. A fulfillment value is never a thenable.
804
-
805
- Promises can also be said to *resolve* a value. If this value is also a
806
- promise, then the original promise's settled state will match the value's
807
- settled state. So a promise that *resolves* a promise that rejects will
808
- itself reject, and a promise that *resolves* a promise that fulfills will
809
- itself fulfill.
810
-
811
-
812
- Basic Usage:
813
- ------------
814
-
815
- ```js
816
- let promise = new Promise(function(resolve, reject) {
817
- // on success
818
- resolve(value);
819
-
820
- // on failure
821
- reject(reason);
822
- });
823
-
824
- promise.then(function(value) {
825
- // on fulfillment
826
- }, function(reason) {
827
- // on rejection
828
- });
829
- ```
830
-
831
- Advanced Usage:
832
- ---------------
833
-
834
- Promises shine when abstracting away asynchronous interactions such as
835
- `XMLHttpRequest`s.
836
-
837
- ```js
838
- function getJSON(url) {
839
- return new Promise(function(resolve, reject){
840
- let xhr = new XMLHttpRequest();
841
-
842
- xhr.open('GET', url);
843
- xhr.onreadystatechange = handler;
844
- xhr.responseType = 'json';
845
- xhr.setRequestHeader('Accept', 'application/json');
846
- xhr.send();
847
-
848
- function handler() {
849
- if (this.readyState === this.DONE) {
850
- if (this.status === 200) {
851
- resolve(this.response);
852
- } else {
853
- reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
854
- }
855
- }
856
- };
857
- });
858
- }
859
-
860
- getJSON('/posts.json').then(function(json) {
861
- // on fulfillment
862
- }, function(reason) {
863
- // on rejection
864
- });
865
- ```
866
-
867
- Unlike callbacks, promises are great composable primitives.
868
-
869
- ```js
870
- Promise.all([
871
- getJSON('/posts'),
872
- getJSON('/comments')
873
- ]).then(function(values){
874
- values[0] // => postsJSON
875
- values[1] // => commentsJSON
876
-
877
- return values;
878
- });
879
- ```
880
-
881
- @class Promise
882
- @param {Function} resolver
883
- Useful for tooling.
884
- @constructor
885
- */
886
-
887
- var Promise$1 = (function () {
888
- function Promise(resolver) {
889
- this[PROMISE_ID] = nextId()
890
- this._result = this._state = undefined
891
- this._subscribers = []
892
-
893
- if (noop !== resolver) {
894
- typeof resolver !== 'function' && needsResolver()
895
- this instanceof Promise ? initializePromise(this, resolver) : needsNew()
896
- }
897
- }
898
-
899
- /**
900
- The primary way of interacting with a promise is through its `then` method,
901
- which registers callbacks to receive either a promise's eventual value or the
902
- reason why the promise cannot be fulfilled.
903
- ```js
904
- findUser().then(function(user){
905
- // user is available
906
- }, function(reason){
907
- // user is unavailable, and you are given the reason why
908
- });
909
- ```
910
- Chaining
911
- --------
912
- The return value of `then` is itself a promise. This second, 'downstream'
913
- promise is resolved with the return value of the first promise's fulfillment
914
- or rejection handler, or rejected if the handler throws an exception.
915
- ```js
916
- findUser().then(function (user) {
917
- return user.name;
918
- }, function (reason) {
919
- return 'default name';
920
- }).then(function (userName) {
921
- // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
922
- // will be `'default name'`
923
- });
924
- findUser().then(function (user) {
925
- throw new Error('Found user, but still unhappy');
926
- }, function (reason) {
927
- throw new Error('`findUser` rejected and we're unhappy');
928
- }).then(function (value) {
929
- // never reached
930
- }, function (reason) {
931
- // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
932
- // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
933
- });
934
- ```
935
- If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
936
- ```js
937
- findUser().then(function (user) {
938
- throw new PedagogicalException('Upstream error');
939
- }).then(function (value) {
940
- // never reached
941
- }).then(function (value) {
942
- // never reached
943
- }, function (reason) {
944
- // The `PedgagocialException` is propagated all the way down to here
945
- });
946
- ```
947
- Assimilation
948
- ------------
949
- Sometimes the value you want to propagate to a downstream promise can only be
950
- retrieved asynchronously. This can be achieved by returning a promise in the
951
- fulfillment or rejection handler. The downstream promise will then be pending
952
- until the returned promise is settled. This is called *assimilation*.
953
- ```js
954
- findUser().then(function (user) {
955
- return findCommentsByAuthor(user);
956
- }).then(function (comments) {
957
- // The user's comments are now available
958
- });
959
- ```
960
- If the assimliated promise rejects, then the downstream promise will also reject.
961
- ```js
962
- findUser().then(function (user) {
963
- return findCommentsByAuthor(user);
964
- }).then(function (comments) {
965
- // If `findCommentsByAuthor` fulfills, we'll have the value here
966
- }, function (reason) {
967
- // If `findCommentsByAuthor` rejects, we'll have the reason here
968
- });
969
- ```
970
- Simple Example
971
- --------------
972
- Synchronous Example
973
- ```javascript
974
- let result;
975
- try {
976
- result = findResult();
977
- // success
978
- } catch(reason) {
979
- // failure
980
- }
981
- ```
982
- Errback Example
983
- ```js
984
- findResult(function(result, err){
985
- if (err) {
986
- // failure
987
- } else {
988
- // success
989
- }
990
- });
991
- ```
992
- Promise Example;
993
- ```javascript
994
- findResult().then(function(result){
995
- // success
996
- }, function(reason){
997
- // failure
998
- });
999
- ```
1000
- Advanced Example
1001
- --------------
1002
- Synchronous Example
1003
- ```javascript
1004
- let author, books;
1005
- try {
1006
- author = findAuthor();
1007
- books = findBooksByAuthor(author);
1008
- // success
1009
- } catch(reason) {
1010
- // failure
1011
- }
1012
- ```
1013
- Errback Example
1014
- ```js
1015
- function foundBooks(books) {
1016
- }
1017
- function failure(reason) {
1018
- }
1019
- findAuthor(function(author, err){
1020
- if (err) {
1021
- failure(err);
1022
- // failure
1023
- } else {
1024
- try {
1025
- findBoooksByAuthor(author, function(books, err) {
1026
- if (err) {
1027
- failure(err);
1028
- } else {
1029
- try {
1030
- foundBooks(books);
1031
- } catch(reason) {
1032
- failure(reason);
1033
- }
1034
- }
1035
- });
1036
- } catch(error) {
1037
- failure(err);
1038
- }
1039
- // success
1040
- }
1041
- });
1042
- ```
1043
- Promise Example;
1044
- ```javascript
1045
- findAuthor().
1046
- then(findBooksByAuthor).
1047
- then(function(books){
1048
- // found books
1049
- }).catch(function(reason){
1050
- // something went wrong
1051
- });
1052
- ```
1053
- @method then
1054
- @param {Function} onFulfilled
1055
- @param {Function} onRejected
1056
- Useful for tooling.
1057
- @return {Promise}
1058
- */
1059
-
1060
- /**
1061
- `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
1062
- as the catch block of a try/catch statement.
1063
- ```js
1064
- function findAuthor(){
1065
- throw new Error('couldn't find that author');
1066
- }
1067
- // synchronous
1068
- try {
1069
- findAuthor();
1070
- } catch(reason) {
1071
- // something went wrong
1072
- }
1073
- // async with promises
1074
- findAuthor().catch(function(reason){
1075
- // something went wrong
1076
- });
1077
- ```
1078
- @method catch
1079
- @param {Function} onRejection
1080
- Useful for tooling.
1081
- @return {Promise}
1082
- */
1083
-
1084
- Promise.prototype.catch = function _catch(onRejection) {
1085
- return this.then(null, onRejection)
1086
- }
1087
-
1088
- /**
1089
- `finally` will be invoked regardless of the promise's fate just as native
1090
- try/catch/finally behaves
1091
-
1092
- Synchronous example:
1093
-
1094
- ```js
1095
- findAuthor() {
1096
- if (Math.random() > 0.5) {
1097
- throw new Error();
1098
- }
1099
- return new Author();
1100
- }
1101
-
1102
- try {
1103
- return findAuthor(); // succeed or fail
1104
- } catch(error) {
1105
- return findOtherAuther();
1106
- } finally {
1107
- // always runs
1108
- // doesn't affect the return value
1109
- }
1110
- ```
1111
-
1112
- Asynchronous example:
1113
-
1114
- ```js
1115
- findAuthor().catch(function(reason){
1116
- return findOtherAuther();
1117
- }).finally(function(){
1118
- // author was either found, or not
1119
- });
1120
- ```
1121
-
1122
- @method finally
1123
- @param {Function} callback
1124
- @return {Promise}
1125
- */
1126
-
1127
- Promise.prototype.finally = function _finally(callback) {
1128
- var promise = this
1129
- var constructor = promise.constructor
1130
-
1131
- if (isFunction(callback)) {
1132
- return promise.then(
1133
- function (value) {
1134
- return constructor.resolve(callback()).then(function () {
1135
- return value
1136
- })
1137
- },
1138
- function (reason) {
1139
- return constructor.resolve(callback()).then(function () {
1140
- throw reason
1141
- })
1142
- }
1143
- )
1144
- }
1145
-
1146
- return promise.then(callback, callback)
1147
- }
1148
-
1149
- return Promise
1150
- })()
1151
-
1152
- Promise$1.prototype.then = then
1153
- Promise$1.all = all
1154
- Promise$1.race = race
1155
- Promise$1.resolve = resolve$1
1156
- Promise$1.reject = reject$1
1157
- Promise$1._setScheduler = setScheduler
1158
- Promise$1._setAsap = setAsap
1159
- Promise$1._asap = asap
1160
- Promise$1.debug = function _debug() {
1161
- return '[object ES6Promise]'
1162
- }
1163
-
1164
- /*global self*/
1165
- function polyfill() {
1166
- var local = globalThis
1167
-
1168
- local.Promise = Promise$1
1169
- }
1170
-
1171
- // Strange compat..
1172
- Promise$1.polyfill = polyfill
1173
- Promise$1.Promise = Promise$1
1174
-
1175
- return Promise$1
1176
- })
1177
-
1178
- //# sourceMappingURL=es6-promise.map