-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1253 from zloirock/promise-with-resolvers
- Loading branch information
Showing
13 changed files
with
177 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
'use strict'; | ||
require('../../modules/es.promise'); | ||
require('../../modules/esnext.promise.with-resolvers'); | ||
var call = require('../../internals/function-call'); | ||
var isCallable = require('../../internals/is-callable'); | ||
var path = require('../../internals/path'); | ||
|
||
var Promise = path.Promise; | ||
var promiseWithResolvers = Promise.withResolvers; | ||
|
||
module.exports = { withResolvers: function withResolvers() { | ||
return call(promiseWithResolvers, isCallable(this) ? this : Promise); | ||
} }.withResolvers; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
var $ = require('../internals/export'); | ||
var newPromiseCapabilityModule = require('../internals/new-promise-capability'); | ||
|
||
// `Promise.withResolvers` method | ||
// https://github.com/tc39/proposal-promise-with-resolvers | ||
$({ target: 'Promise', stat: true, forced: true }, { | ||
withResolvers: function withResolvers() { | ||
var promiseCapability = newPromiseCapabilityModule.f(this); | ||
return { | ||
promise: promiseCapability.promise, | ||
resolve: promiseCapability.resolve, | ||
reject: promiseCapability.reject | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// https://github.com/tc39/proposal-promise-with-resolvers | ||
require('../modules/esnext.promise.with-resolvers'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { getPrototypeOf } = Object; | ||
|
||
QUnit.test('Promise.withResolvers', assert => { | ||
const { withResolvers } = Promise; | ||
assert.isFunction(withResolvers); | ||
assert.arity(withResolvers, 0); | ||
assert.name(withResolvers, 'withResolvers'); | ||
assert.nonEnumerable(Promise, 'withResolvers'); | ||
assert.looksNative(withResolvers); | ||
|
||
const d1 = Promise.withResolvers(); | ||
assert.same(getPrototypeOf(d1), Object.prototype, 'proto is Object.prototype'); | ||
assert.true(d1.promise instanceof Promise, 'promise is promise'); | ||
assert.isFunction(d1.resolve, 'resolve is function'); | ||
assert.isFunction(d1.reject, 'reject is function'); | ||
|
||
const promise = {}; | ||
const resolve = () => { /* empty */ }; | ||
let reject = () => { /* empty */ }; | ||
|
||
function P(exec) { | ||
exec(resolve, reject); | ||
return promise; | ||
} | ||
|
||
const d2 = withResolvers.call(P); | ||
assert.same(d2.promise, promise, 'promise is promise #2'); | ||
assert.same(d2.resolve, resolve, 'resolve is resolve #2'); | ||
assert.same(d2.reject, reject, 'reject is reject #2'); | ||
|
||
reject = {}; | ||
|
||
assert.throws(() => withResolvers.call(P), TypeError, 'broken resolver'); | ||
assert.throws(() => withResolvers.call({}), TypeError, 'broken constructor #1'); | ||
assert.throws(() => withResolvers.call(null), TypeError, 'broken constructor #2'); | ||
}); | ||
|
||
QUnit.test('Promise.withResolvers, resolve', assert => { | ||
const d = Promise.withResolvers(); | ||
d.resolve(42); | ||
return d.promise.then(it => { | ||
assert.same(it, 42, 'resolved as expected'); | ||
}, () => { | ||
assert.avoid(); | ||
}); | ||
}); | ||
|
||
QUnit.test('Promise.withResolvers, reject', assert => { | ||
const d = Promise.withResolvers(); | ||
d.reject(42); | ||
return d.promise.then(() => { | ||
assert.avoid(); | ||
}, error => { | ||
assert.same(error, 42, 'rejected as expected'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import Promise from 'core-js-pure/full/promise'; | ||
import getPrototypeOf from 'core-js-pure/es/object/get-prototype-of'; | ||
|
||
QUnit.test('Promise.withResolvers', assert => { | ||
const { withResolvers } = Promise; | ||
assert.isFunction(withResolvers); | ||
assert.arity(withResolvers, 0); | ||
assert.name(withResolvers, 'withResolvers'); | ||
|
||
const d1 = Promise.withResolvers(); | ||
assert.same(getPrototypeOf(d1), Object.prototype, 'proto is Object.prototype'); | ||
assert.true(d1.promise instanceof Promise, 'promise is promise'); | ||
assert.isFunction(d1.resolve, 'resolve is function'); | ||
assert.isFunction(d1.reject, 'reject is function'); | ||
|
||
const promise = {}; | ||
const resolve = () => { /* empty */ }; | ||
let reject = () => { /* empty */ }; | ||
|
||
function P(exec) { | ||
exec(resolve, reject); | ||
return promise; | ||
} | ||
|
||
const d2 = withResolvers.call(P); | ||
assert.same(d2.promise, promise, 'promise is promise #2'); | ||
assert.same(d2.resolve, resolve, 'resolve is resolve #2'); | ||
assert.same(d2.reject, reject, 'reject is reject #2'); | ||
|
||
reject = {}; | ||
|
||
assert.throws(() => withResolvers.call(P), TypeError, 'broken resolver'); | ||
assert.throws(() => withResolvers.call({}), TypeError, 'broken constructor #1'); | ||
assert.throws(() => withResolvers.call(null), TypeError, 'broken constructor #2'); | ||
}); | ||
|
||
QUnit.test('Promise.withResolvers, resolve', assert => { | ||
const d = Promise.withResolvers(); | ||
d.resolve(42); | ||
return d.promise.then(it => { | ||
assert.same(it, 42, 'resolved as expected'); | ||
}, () => { | ||
assert.avoid(); | ||
}); | ||
}); | ||
|
||
QUnit.test('Promise.withResolvers, reject', assert => { | ||
const d = Promise.withResolvers(); | ||
d.reject(42); | ||
return d.promise.then(() => { | ||
assert.avoid(); | ||
}, error => { | ||
assert.same(error, 42, 'rejected as expected'); | ||
}); | ||
}); |