main
Last change
on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago |
Update repo after prototype presentation
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d565449] | 1 | 'use strict';
|
---|
| 2 |
|
---|
| 3 | var $TypeError = require('es-errors/type');
|
---|
| 4 |
|
---|
| 5 | var IsCallable = require('./IsCallable');
|
---|
| 6 | var IsConstructor = require('./IsConstructor');
|
---|
| 7 |
|
---|
| 8 | // https://262.ecma-international.org/6.0/#sec-newpromisecapability
|
---|
| 9 |
|
---|
| 10 | module.exports = function NewPromiseCapability(C) {
|
---|
| 11 | if (!IsConstructor(C)) {
|
---|
| 12 | throw new $TypeError('C must be a constructor'); // step 1
|
---|
| 13 | }
|
---|
| 14 |
|
---|
| 15 | var resolvingFunctions = { '[[Resolve]]': void undefined, '[[Reject]]': void undefined }; // step 3
|
---|
| 16 |
|
---|
| 17 | var promise = new C(function (resolve, reject) { // steps 4-5
|
---|
| 18 | if (typeof resolvingFunctions['[[Resolve]]'] !== 'undefined' || typeof resolvingFunctions['[[Reject]]'] !== 'undefined') {
|
---|
| 19 | throw new $TypeError('executor has already been called'); // step 4.a, 4.b
|
---|
| 20 | }
|
---|
| 21 | resolvingFunctions['[[Resolve]]'] = resolve; // step 4.c
|
---|
| 22 | resolvingFunctions['[[Reject]]'] = reject; // step 4.d
|
---|
| 23 | }); // step 4-6
|
---|
| 24 |
|
---|
| 25 | if (!IsCallable(resolvingFunctions['[[Resolve]]']) || !IsCallable(resolvingFunctions['[[Reject]]'])) {
|
---|
| 26 | throw new $TypeError('executor must provide valid resolve and reject functions'); // steps 7-8
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | return {
|
---|
| 30 | '[[Promise]]': promise,
|
---|
| 31 | '[[Resolve]]': resolvingFunctions['[[Resolve]]'],
|
---|
| 32 | '[[Reject]]': resolvingFunctions['[[Reject]]']
|
---|
| 33 | }; // step 9
|
---|
| 34 | };
|
---|
Note:
See
TracBrowser
for help on using the repository browser.