• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DigitalBrainJS / AxiosPromise / 4876413558

pending completion
4876413558

Pull #43

github

GitHub
Merge c58abd04e into bc051d135
Pull Request #43: feat(refactor): rename addSignature method to addSignatureTo;

199 of 247 branches covered (80.57%)

Branch coverage included in aggregate %.

17 of 17 new or added lines in 2 files covered. (100.0%)

874 of 1104 relevant lines covered (79.17%)

421.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.94
/lib/index.js
1
import utils from './utils.js';
1✔
2
import {CanceledError} from "./CanceledError.js";
1✔
3
import {TimeoutError} from "./TimeoutError.js";
1✔
4
import {AbortController, AbortSignal} from "./AbortController.js"
1✔
5
import EventEmitter from "./EventEmitter.js";
1✔
6
import {VERSION} from "./env/data.js";
1✔
7

1✔
8
const {
1✔
9
  isGenerator,
1✔
10
  isFunction,
1✔
11
  isGeneratorFunction,
1✔
12
  isAsyncFunction,
1✔
13
  isPlainFunction,
1✔
14
  isContextDefined,
1✔
15
  lazyBind,
1✔
16
  defineConstants,
1✔
17
  symbols,
1✔
18
  isAbortSignal,
1✔
19
  global,
1✔
20
  setImmediate,
1✔
21
  isAbortController,
1✔
22
  asap
1✔
23
} = utils;
1✔
24

1✔
25
const kPromiseSign = Symbol.for('AxiosPromise');
1✔
26

1✔
27
const [
1✔
28
  kState,
1✔
29
  kValue,
1✔
30
  kCallbacks,
1✔
31
  kDoResolve,
1✔
32
  kResolveTo,
1✔
33
  kResolve,
1✔
34
  kParent,
1✔
35
  kInnerThenable,
1✔
36
  kCanceled,
1✔
37
  kFinalized,
1✔
38
  kSync,
1✔
39
  kCallback,
1✔
40
  kCancelCallbacks,
1✔
41
  kInternals,
1✔
42
  kFinalize,
1✔
43
  kUnsubscribe,
1✔
44
  kResolveGenerator,
1✔
45
  kAtomic,
1✔
46
  kCanceledWith,
1✔
47
  kTag,
1✔
48
  kTimer,
1✔
49
  kUnhandledFlag
1✔
50
] = symbols(
1✔
51
  'state',
1✔
52
  'value',
1✔
53
  'callbacks',
1✔
54
  'doResolve',
1✔
55
  'resolveTo',
1✔
56
  'resolve',
1✔
57
  'parent',
1✔
58
  'innerThenable',
1✔
59
  'canceled',
1✔
60
  'finalized',
1✔
61
  'sync',
1✔
62
  'callback',
1✔
63
  'cancelCallbacks',
1✔
64
  'internals',
1✔
65
  'finalize',
1✔
66
  'unsubscribe',
1✔
67
  'resolveGenerator',
1✔
68
  'atomic',
1✔
69
  'canceledWith',
1✔
70
  'tag',
1✔
71
  'timer',
1✔
72
  'unhandledFlag'
1✔
73
);
1✔
74

1✔
75
const STATE_PENDING = 0;
1✔
76
const STATE_FULFILLED = 1;
1✔
77
const STATE_REJECTED = 2;
1✔
78

1✔
79
const ATOMIC_MODE_AWAIT = false;
1✔
80
const ATOMIC_MODE_DETACHED = true;
1✔
81

1✔
82
const {push} = Array.prototype;
1✔
83

1✔
84
const hasConsole = typeof console !== 'undefined' && console;
1✔
85

1✔
86
const noop = () => {};
1✔
87

1✔
88
const getMethod = (obj, name) => {
1✔
89
  let type;
2,468✔
90
  let then;
2,468✔
91

2,468✔
92
  if(((type = typeof obj) === 'object' || type === 'function') && typeof (then = obj[name]) === 'function') {
2,468✔
93
    return then;
1,438✔
94
  }
1,438✔
95
}
2,468✔
96

1✔
97
const invokeCallbacks = (callbacks, args, that) => {
1✔
98
  if(!callbacks) return false;
32!
99

32✔
100
  if (typeof callbacks === 'function') {
32!
101
    callbacks.apply(that, args);
×
102
    return true;
×
103
  } else {
32✔
104
    const {length} = callbacks;
32✔
105
    for (let i = 0; i < length; i++) {
32✔
106
      callbacks[i].apply(that, args);
32✔
107
    }
32✔
108
    return !!length;
32✔
109
  }
32✔
110
}
32✔
111

1✔
112
const [trackUnhandled, untrackUnhandled] = ((trackQueue, length) => {
1✔
113
  let timer;
1✔
114

1✔
115
  const handler = () => {
1✔
116
    timer = 0;
19✔
117
    let l = trackQueue.length;
19✔
118
    let p;
19✔
119
    for (let i = 0; i < l; i++) {
19✔
120
      (p = trackQueue[i]).constructor._unhandledRejection(p[kValue], p);
14✔
121
    }
14✔
122
    trackQueue = [];
19✔
123
  }
19✔
124

1✔
125
  return [(promise) => {
1✔
126
    trackQueue.push(promise);
22✔
127
    length++;
22✔
128
    timer || (timer = setTimeout(handler));
22✔
129
  }, (promise) => {
1✔
130
    const index = trackQueue.indexOf(promise);
12✔
131
    if (index < 0) return;
12✔
132
    length--;
8✔
133
    if (!length) {
12!
134
      timer && clearTimeout(timer);
×
135
      timer = 0;
×
136
      trackQueue = [];
×
137
    } else {
12✔
138
      trackQueue.splice(index, 1);
8✔
139
    }
8✔
140
  }];
1✔
141
})([], 0);
1✔
142

1✔
143
export class AxiosPromise{
1✔
144
  constructor(executor, {signal, timeout} = {}) {
1✔
145
    this[kState] = STATE_PENDING;
3,434✔
146
    this[kCallbacks] = null;
3,434✔
147
    this[kInternals] = {
3,434✔
148
      signals: null
3,434✔
149
    }
3,434✔
150

3,434✔
151
    if(signal) {
3,434!
152
      if(signal.aborted) {
×
153
        this.cancel();
×
154
        return this;
×
155
      }
×
156

×
157
      this.listen(signal);
×
158
    }
×
159

3,434✔
160
    timeout && this.timeout(timeout);
3,434!
161

3,434✔
162
    let type;
3,434✔
163

3,434✔
164
    if(executor !== noop) {
3,434✔
165
      if ((type = typeof executor) !== 'function') {
3,430!
166
        this[kResolveTo](new TypeError(`Promise resolver ${type} is not a function`));
×
167
        return this;
×
168
      }
×
169

3,430✔
170
      const maybeOnCancelSubscriber = this[kDoResolve](executor);
3,430✔
171

3,430✔
172
      if (maybeOnCancelSubscriber && isFunction(maybeOnCancelSubscriber)) {
3,430✔
173
        this.onCancel(maybeOnCancelSubscriber);
2✔
174
      }
2✔
175
    }
3,430✔
176
  }
3,434✔
177

1✔
178
  [kDoResolve](fn, fnContext) {
1✔
179
    let done = false;
4,872✔
180

4,872✔
181
    try {
4,872✔
182
      return fn.call(fnContext,
4,872✔
183
        (value) => {
4,872✔
184
          if (done) return;
1,899✔
185
          done = true;
1,783✔
186
          this[kResolve](value, false);
1,783✔
187
        }, (reason) => {
4,872✔
188
          if (done) return;
869✔
189
          done = true;
831✔
190
          this[kResolveTo](reason, true);
831✔
191
        }, this);
4,872✔
192
    } catch (e) {
4,872✔
193
      if (done) return;
138✔
194
      done = true;
38✔
195
      this[kResolveTo](e, true);
38✔
196
    }
38✔
197
  }
4,872✔
198

1✔
199
  timeout(ms, errorOrMessage) {
1✔
200
    if (!this[kState]) {
2✔
201
      if (this[kTimer]) {
2!
202
        clearTimeout(this[kTimer]);
×
203
        this[kTimer] = 0;
×
204
      }
×
205

2✔
206
      if (ms > 0) {
2✔
207
        this[kTimer] = setTimeout(() => {
2✔
208
          this[kTimer] = 0;
2✔
209
          this.cancel(TimeoutError.from(errorOrMessage || ms))
2✔
210
        }, ms);
2✔
211
      }
2✔
212
    }
2✔
213
    return this;
2✔
214
  }
2✔
215

1✔
216
  tag(tag) {
1✔
217
    if (!arguments.length) {
×
218
      return this[kTag] || '';
×
219
    }
×
220
    this[kTag] = String(tag);
×
221
    return this;
×
222
  }
×
223

1✔
224
  atomic(mode = ATOMIC_MODE_AWAIT) {
1✔
225
    const promise = this.then();
4✔
226

4✔
227
    promise[kAtomic] = (this[kAtomic] = !!mode);
4✔
228

4✔
229
    return promise;
4✔
230
  }
4✔
231

1✔
232
  static atomic(chain, mode = ATOMIC_MODE_AWAIT) {
1✔
233
    return this.resolve(chain, {atomic: mode})
×
234
  }
×
235

1✔
236
  cancel(reason, forced) {
1✔
237
    if (this[kFinalized]) return false;
62!
238

62✔
239
    let target = this;
62✔
240
    let parent;
62✔
241
    let atomic = target[kAtomic];
62✔
242

62✔
243
    while (atomic === undefined && (parent = target[kParent]) && !parent[kFinalized] && (forced || typeof parent[kCallbacks] === 'function' || parent[kCallbacks].length <= 1)) {
62!
244
      atomic = parent[kAtomic]
30✔
245
      target = parent;
30✔
246
    }
30✔
247

62✔
248
    reason = CanceledError.from(reason);
62✔
249

62✔
250
    if (atomic === ATOMIC_MODE_AWAIT) {
62✔
251
      target[kCanceledWith] = reason;
2✔
252
      return true;
2✔
253
    }
2✔
254

60✔
255
    const innerThenable = target[kInnerThenable];
60✔
256

60✔
257
    if (!(innerThenable && typeof innerThenable.cancel === 'function' && innerThenable.cancel(reason)) && !target[kCanceled]) {
62✔
258
      reason.scope = target;
40✔
259
      target[kResolveTo](reason, true);
40✔
260
    }
40✔
261

60✔
262
    return true;
60✔
263
  }
62✔
264

1✔
265
  listen(signal) {
1✔
266
    if(!this[kFinalized]) {
2✔
267
      if (!isAbortSignal(signal)) {
2!
268
        throw TypeError('expected AbortSignal object');
×
269
      }
×
270

2✔
271
      const internals = this[kInternals];
2✔
272

2✔
273
      if (internals.signals) {
2!
274
        internals.signals.push(signal);
×
275
      } else {
2✔
276
        internals.signals = [signal];
2✔
277
      }
2✔
278

2✔
279
      signal.addEventListener('abort', () => this.cancel());
2✔
280
    }
2✔
281

2✔
282
    return this;
2✔
283
  }
2✔
284

1✔
285
  onCancel(listener) {
1✔
286
    this[kCancelCallbacks] ? this[kCancelCallbacks].push(listener) : this[kCancelCallbacks] = [listener];
68!
287
  }
68✔
288

1✔
289
  get signal(){
1✔
290
    return (this[kInternals].controller = new AbortController()).signal;
2✔
291
  }
2✔
292

1✔
293
  [kUnsubscribe](chain) {
1✔
294
    const parentCallbacks = this[kCallbacks];
2✔
295
    const callback = chain[kCallback];
2✔
296
    if(typeof parentCallbacks === 'function' && parentCallbacks === callback) {
2✔
297
      this[kCallbacks] = null;
2✔
298
    } else {
2!
299
      let i = parentCallbacks.length;
×
300
      while (i--) {
×
301
        if (parentCallbacks[i] === callback) {
×
302
          return parentCallbacks.splice(i, 1);
×
303
        }
×
304
      }
×
305
    }
×
306
  }
2✔
307

1✔
308
  [kFinalize](){
1✔
309
    this[kFinalized] = true;
3,420✔
310

3,420✔
311
    let value = this[kValue];
3,420✔
312
    let isRejected = this[kState] === STATE_REJECTED;
3,420✔
313
    let canceled = this[kCanceled];
3,420✔
314
    const internals = this[kInternals];
3,420✔
315
    const {signals, controller} = internals;
3,420✔
316
    const parent = this[kParent];
3,420✔
317

3,420✔
318
    if (!isRejected && this[kCanceledWith]) {
3,420✔
319
      canceled = true;
2✔
320
      isRejected = true;
2✔
321
      value = this[kCanceledWith];
2✔
322
    }
2✔
323

3,420✔
324
    if (parent && !parent[kFinalized]) { // Premature resolving - unsubscribe from parent chain
3,420✔
325
      parent[kUnsubscribe](this);
2✔
326
    }
2✔
327

3,420✔
328
    this[kTimer] && clearTimeout(this[kTimer]);
3,420!
329

3,420✔
330
    if (signals) {
3,420✔
331
      let i = signals.length;
2✔
332
      while (i--) {
2✔
333
        signals[i].removeEventListener('abort', this.cancel);
2✔
334
      }
2✔
335
    }
2✔
336

3,420✔
337
    if(canceled){
3,420✔
338
      const cancelCallbacks = this[kCancelCallbacks];
66✔
339

66✔
340
      if (cancelCallbacks) {
66✔
341
        invokeCallbacks(cancelCallbacks, [value]);
32✔
342
      }
32✔
343

66✔
344
      controller && controller.abort();
66✔
345
    }
66✔
346

3,420✔
347
    const callbacks = this[kCallbacks];
3,420✔
348

3,420✔
349
    let hasCallback;
3,420✔
350

3,420✔
351
    if(callbacks) {
3,420✔
352
      if (typeof callbacks === 'function') {
2,027✔
353
        hasCallback = true;
1,987✔
354
        callbacks(value, isRejected);
1,987✔
355
      } else {
2,027✔
356
        const {length} = callbacks;
40✔
357
        hasCallback = length;
40✔
358
        for (let i = 0; i < length; i++) {
40✔
359
          callbacks[i](value, isRejected);
130✔
360
        }
130✔
361
      }
40✔
362
    }
2,027✔
363

3,420✔
364
    if(isRejected && !hasCallback && !canceled) {
3,420✔
365
      if (hasConsole && this[kAtomic] !== ATOMIC_MODE_DETACHED) {
22✔
366
        this[kUnhandledFlag] = true;
22✔
367
        trackUnhandled(this);
22✔
368
      }
22✔
369
    }
22✔
370

3,420✔
371
    this[kCallbacks] = null;
3,420✔
372
    this[kParent] = null;
3,420✔
373
    this[kCancelCallbacks] = null;
3,420✔
374
    this[kInnerThenable] = null;
3,420✔
375
    this[kInternals] = null;
3,420✔
376
    this[kTimer] = null;
3,420✔
377
  }
3,420✔
378

1✔
379
  [kResolveTo](value, isRejected) {
1✔
380
    if (this[kFinalized]) return;
3,430✔
381

3,422✔
382
    const settled = this[kState];
3,422✔
383

3,422✔
384
    if (isRejected && CanceledError.isCanceledError(value)) {
3,430✔
385
      this[kCanceled] = true;
64✔
386
    } else if (settled) {
3,430✔
387
      return;
2✔
388
    }
2✔
389

3,420✔
390
    this[kValue] = value;
3,420✔
391

3,420✔
392
    if (!settled) {
3,420✔
393
      this[kState] = isRejected ? STATE_REJECTED : STATE_FULFILLED;
3,420✔
394

3,420✔
395
      this[kSync] ? this[kFinalize]() : asap(() => this[kFinalize]());
3,420✔
396
    }
3,420✔
397
  }
3,430✔
398

1✔
399
  [kResolve](value, isRejected) {
1✔
400
    const {constructor} = this;
3,881✔
401
    let then;
3,881✔
402

3,881✔
403
    if (this === value) {
3,881✔
404
      return this[kResolveTo](new TypeError(`circular reference to ${value}`), true);
2✔
405
    }
2✔
406

3,879✔
407
    if (value && isGenerator(value)) {
3,881✔
408
      then = (value = constructor[kResolveGenerator](value, new constructor(noop))).then;
4✔
409
    } else if (value) {
3,881✔
410
      try {
2,468✔
411
        then = getMethod(value, 'then');
2,468✔
412
      } catch (err) {
2,468✔
413
        return this[kResolveTo](err, true);
54✔
414
      }
54✔
415
    }
2,468✔
416

3,825✔
417
    if (then && typeof then === 'function') {
3,881✔
418
      this[kInnerThenable] = value;
1,442✔
419
      this[kDoResolve](then, value);
1,442✔
420
    } else {
3,881✔
421
      this[kResolveTo](value, isRejected)
2,383✔
422
    }
2,383✔
423

3,881✔
424
  }
3,881✔
425

1✔
426
  then(onFulfilled, onRejected) {
1✔
427
    onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : null;
2,179✔
428
    onRejected = typeof onRejected === 'function' ? onRejected : null;
2,179✔
429

2,179✔
430
    this[kUnhandledFlag] && untrackUnhandled(this);
2,179✔
431

2,179✔
432
    return new this.constructor(($, $$, promise) => {
2,179✔
433
      let invoked;
2,179✔
434

2,179✔
435
      const resolver = (value, isRejected) => {
2,179✔
436
        if (invoked) return;
2,170!
437

2,170✔
438
        invoked = true;
2,170✔
439

2,170✔
440
        const handler = isRejected ? onRejected : onFulfilled;
2,170✔
441

2,170✔
442
        try {
2,170✔
443
          promise[kResolve](handler ? handler(value, promise) : value, isRejected && !handler);
2,170✔
444
        } catch (err) {
2,170✔
445
          promise[kResolveTo](err, true);
80✔
446
        }
80✔
447
      };
2,179✔
448

2,179✔
449
      promise[kParent] = this;
2,179✔
450

2,179✔
451
      if (this[kFinalized]) {
2,179✔
452
        return this[kSync] ?
53✔
453
          resolver(this[kValue], this[kState] === STATE_REJECTED) :
53✔
454
          asap(() => {
41✔
455
            resolver(this[kValue], this[kState] === STATE_REJECTED)
41✔
456
          });
53✔
457
      }
53✔
458

2,126✔
459
      promise[kCallback] = resolver;
2,126✔
460

2,126✔
461
      const callbacks = this[kCallbacks];
2,126✔
462

2,126✔
463
      if (callbacks) {
2,179✔
464
        typeof callbacks === 'function' ? this[kCallbacks] = [callbacks, resolver] : callbacks.push(resolver);
90✔
465
      } else {
2,179✔
466
        this[kCallbacks] = resolver;
2,036✔
467
      }
2,036✔
468
    });
2,179✔
469
  }
2,179✔
470

1✔
471
  catch(onRejected) {
1✔
472
    return this.then(null, onRejected);
2✔
473
  }
2✔
474

1✔
475
  finally(callback) {
1✔
476
    let {constructor} = this;
×
477

×
478
    return isFunction(callback) ? this.then(
×
479
      (value, scope) => constructor.resolve(callback({value, status: 'fulfilled'}, scope)).then(() => value),
×
480
      (reason, scope) => constructor.resolve(callback({reason, status: 'rejected'}, scope)).then(() => {
×
481
        throw reason;
×
482
      })) : this.then(callback, callback);
×
483
  }
×
484

1✔
485
  static allSettled(promises) {
1✔
486
    return this.all(Array.from(promises).map(promise => promise.then(value => ({
×
487
        status: 'fulfilled', value
×
488
      }), (reason) => ({
×
489
        status: 'rejected', reason
×
490
      }))
×
491
    ))
×
492
  }
×
493

1✔
494
  get [Symbol.toStringTag](){
1✔
495
    return 'AxiosPromise';
2✔
496
  }
2✔
497

1✔
498
  toString() {
1✔
499
    const tag = this[kTag];
2✔
500
    return `${this[Symbol.toStringTag]}${tag ? '#' + tag : ''}{${['pending', 'fulfilled', 'rejected'][this[kState]]}}`
2!
501
  }
2✔
502

1✔
503
  static resolve(value, options) {
1✔
504
    const atomic = options != null ? !!options.atomic : undefined;
483!
505

483✔
506
    if (value instanceof this && value[kSync] === this[kSync] && value[kAtomic] === atomic) {
483!
507
      return value;
×
508
    }
×
509
    const promise = new this(resolve => resolve(value));
483✔
510

483✔
511
    atomic && (promise[kAtomic] = atomic);
483!
512

483✔
513
    return promise;
483✔
514
  }
483✔
515

1✔
516
  static reject(reason) {
1✔
517
    return new this((_, reject) => reject(reason));
395✔
518
  }
395✔
519

1✔
520
  static all(promises) {
1✔
521
    return new this((resolve, reject, {onCancel}) => {
4✔
522
      const {length} = promises = Array.from(promises);
4✔
523

4✔
524
      if (!length) {
4!
525
        resolve([]);
×
526
        return;
×
527
      }
×
528

4✔
529
      const chains = new Array(length);
4✔
530
      const results = new Array(length);
4✔
531
      let cancelRequested;
4✔
532
      let subscribed;
4✔
533
      let canceled;
4✔
534
      let counter = 0;
4✔
535

4✔
536
      const cancel = (reason) => {
4✔
537
        if (subscribed) {
4✔
538
          canceled = true;
4✔
539
          for (let i = 0; i < length; i++) {
4✔
540
            chains[i].cancel(reason)
8✔
541
          }
8✔
542
        } else {
4!
543
          cancelRequested = true;
×
544
        }
×
545
      }
4✔
546

4✔
547
      const _reject = (reason) => {
4✔
548
        reject(reason);
8✔
549
        !canceled && cancel();
8✔
550
      };
4✔
551

4✔
552
      for (let i = 0; i < length; i++) {
4✔
553
        chains[i] = this.resolve(promises[i]).then((value) => {
8✔
554
          results[i] = value;
×
555
          if (++counter === length) {
×
556
            resolve(value);
×
557
          }
×
558
        }, _reject);
8✔
559
      }
8✔
560

8✔
561
      subscribed = true;
8✔
562

8✔
563
      cancelRequested ? cancel() : onCancel(cancel);
4!
564
    })
4✔
565
  }
4✔
566

1✔
567
  static race(promises) {
1✔
568
    return new this((resolve, reject, {onCancel}) => {
4✔
569
      const {length} = promises = Array.from(promises);
4✔
570

4✔
571
      if (!length) {
4!
572
        return;
×
573
      }
×
574

4✔
575
      const chains = new Array(length);
4✔
576
      let cancelRequested;
4✔
577
      let subscribed;
4✔
578
      let canceled;
4✔
579

4✔
580
      const cancel = (reason) => {
4✔
581
        if (subscribed) {
4✔
582
          canceled = true;
4✔
583
          for (let i = 0; i < length; i++) {
4✔
584
            chains[i].cancel(reason)
8✔
585
          }
8✔
586
        } else {
4!
587
          cancelRequested = true;
×
588
        }
×
589
      }
4✔
590

4✔
591
      const _reject = (reason) => {
4✔
592
        try {
6✔
593
          reject(reason);
6✔
594
        } finally {
6✔
595
          !canceled && cancel();
6!
596
        }
6✔
597
      };
4✔
598

4✔
599
      for (let i = 0; i < length; i++) {
4✔
600
        chains[i] = this.resolve(promises[i]).then((value) => {
8✔
601
          resolve(value);
2✔
602
          !canceled && cancel();
2✔
603
        }, _reject);
8✔
604
      }
8✔
605

8✔
606
      subscribed = true;
8✔
607

8✔
608
      cancelRequested ? cancel() : onCancel(cancel);
4!
609
    })
4✔
610
  }
4✔
611

1✔
612
  static delay(ms, value) {
1✔
613
    return new this((resolve, _, {onCancel})=> {
6✔
614
      const timer = setTimeout(resolve, ms, value);
6✔
615
      onCancel(() => clearTimeout(timer));
6✔
616
    });
6✔
617
  }
6✔
618

1✔
619
  static isCanceledError(thing) {
1✔
620
    return CanceledError.isCanceledError(thing);
36✔
621
  }
36✔
622

1✔
623
  static isAxiosPromise(thing) {
1✔
624
    return !!(thing && thing[kPromiseSign]);
×
625
  }
×
626

1✔
627
  static [kResolveGenerator](generator, promise) {
1✔
628
    const onFulfilled = (result) => {
10✔
629
      try {
18✔
630
        next(generator.next(result));
18✔
631
      } catch (e) {
18!
632
        promise[kResolveTo](e, true)
×
633
      }
×
634
    }
18✔
635

10✔
636
    const onRejected = (err) => {
10✔
637
      try {
2✔
638
        next(generator.throw(err));
2✔
639
      } catch (e) {
2✔
640
        promise[kResolveTo](e, true);
2✔
641
      }
2✔
642
    }
2✔
643

10✔
644
    const next = (r) => {
10✔
645
      if (r.done) {
18✔
646
        return promise[kResolve](r.value);
8✔
647
      }
8✔
648

10✔
649
      const innerPromise = this.resolve(r.value).then(onFulfilled, onRejected);
10✔
650

10✔
651
      promise[kInnerThenable] = innerPromise;
10✔
652

10✔
653
      return innerPromise;
10✔
654
    }
18✔
655

10✔
656
    onFulfilled();
10✔
657

10✔
658
    return promise;
10✔
659
  }
10✔
660

1✔
661
  static _unhandledRejection(reason) {
1✔
662
    const source = this[kTag] ? ` @ ${this[kTag]}` : '';
12!
663
    console.warn(`Unhandled AxiosPromise Rejection${source}: ` + reason);
12✔
664
  }
12✔
665

1✔
666
  static promisify(fn, {scopeArg = false, scopeContext = false, passthrough = true} = {}) {
1✔
667
    if (fn && fn[kPromiseSign]) return fn;
6!
668

6✔
669
    if (!isGeneratorFunction(fn)) {
6!
670
      if (passthrough) {
×
671
        if (!isFunction(fn)) {
×
672
          throw TypeError('value must be a function');
×
673
        }
×
674
        return fn;
×
675
      }
×
676
      throw new TypeError(`value must be a generator function`);
×
677
    }
×
678

6✔
679
    const context = this;
6✔
680

6✔
681
    const asyncFn = function () {
6✔
682
      return new context((resolve, reject, scope) => {
6✔
683
        let generatorArgs;
6✔
684
        if (scopeArg) {
6!
685
          generatorArgs = [scope];
×
686
          push.apply(generatorArgs, arguments);
×
687
        } else {
6✔
688
          generatorArgs = arguments;
6✔
689
        }
6✔
690

6✔
691
        context[kResolveGenerator](fn.apply(scopeContext || !this || this === global ? scope : this, generatorArgs), scope)
6✔
692
      });
6✔
693
    };
6✔
694

6✔
695
    asyncFn[kPromiseSign] = true;
6✔
696

6✔
697
    return asyncFn;
6✔
698
  }
6✔
699

1✔
700
  static promisifyAll(obj, {reducer, ...options} = {}) {
1✔
701
    Object.entries(obj).forEach(([key, value]) => {
4✔
702
      const descriptor = Object.getOwnPropertyDescriptor(obj, key);
4✔
703
      let ret;
4✔
704
      'value' in descriptor && (!reducer || (ret = reducer.call(obj, key, value))) && isGeneratorFunction(value) &&
4✔
705
      Object.defineProperty(obj, ret && typeof key ==='string' ? ret : key, {
4✔
706
        ...descriptor,
4✔
707
        value: this.promisify(value, options)
4✔
708
      });
4✔
709
    });
4✔
710
  }
4✔
711
}
1✔
712

1✔
713
const {prototype} = AxiosPromise;
1✔
714

1✔
715
prototype[kSync] = false;
1✔
716

1✔
717
lazyBind(prototype, ['cancel', 'onCancel', 'signal']);
1✔
718
lazyBind(AxiosPromise,['delay', 'promisify']);
1✔
719

1✔
720
defineConstants(AxiosPromise, {
1✔
721
  VERSION,
1✔
722
  AbortController,
1✔
723
  AbortSignal,
1✔
724
  CanceledError,
1✔
725
  TimeoutError,
1✔
726
});
1✔
727

1✔
728
export class AxiosPromiseSync extends AxiosPromise {
1✔
729
  get [Symbol.toStringTag](){
1✔
730
    return 'AxiosPromiseSync';
×
731
  }
×
732
}
1✔
733

1✔
734
AxiosPromiseSync.prototype[kSync] = true;
1✔
735

1✔
736
export {
1✔
737
  isGenerator,
1✔
738
  isGeneratorFunction,
1✔
739
  isAsyncFunction,
1✔
740
  isPlainFunction,
1✔
741
  isContextDefined,
1✔
742
  lazyBind,
1✔
743
  defineConstants,
1✔
744
  symbols,
1✔
745
  isAbortSignal,
1✔
746
  global,
1✔
747
  isAbortController,
1✔
748
  setImmediate,
1✔
749
  asap,
1✔
750
  AbortSignal,
1✔
751
  AbortController,
1✔
752
  EventEmitter,
1✔
753
  CanceledError,
1✔
754
  TimeoutError,
1✔
755
  AxiosPromise as default
1✔
756
};
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc