-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* extract create-promise * Update lib/create-promise.js Co-authored-by: Matteo Collina <[email protected]> --------- Co-authored-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
3 changed files
with
90 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
/** | ||
* @typedef PromiseObject | ||
* @property {Promise} promise | ||
* @property {PromiseConstructor["resolve"]} resolve | ||
* @property {PromiseConstructor["reject"]} reject | ||
*/ | ||
|
||
/** | ||
* @returns {PromiseObject} | ||
*/ | ||
function createPromise () { | ||
/** | ||
* @type {PromiseObject} | ||
*/ | ||
const obj = { | ||
resolve: null, | ||
reject: null, | ||
promise: null | ||
} | ||
|
||
obj.promise = new Promise((resolve, reject) => { | ||
obj.resolve = resolve | ||
obj.reject = reject | ||
}) | ||
|
||
return obj | ||
} | ||
|
||
module.exports = { | ||
createPromise | ||
} |
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,55 @@ | ||
'use strict' | ||
|
||
const { test } = require('tap') | ||
const { createPromise } = require('../../lib/create-promise') | ||
|
||
test('createPromise() returns an object', (t) => { | ||
t.plan(3) | ||
t.type(createPromise(), 'object') | ||
t.equal(Array.isArray(createPromise()), false) | ||
t.notOk(Array.isArray(createPromise() !== null)) | ||
}) | ||
|
||
test('createPromise() returns an attribute with attribute resolve', (t) => { | ||
t.plan(1) | ||
t.ok('resolve' in createPromise()) | ||
}) | ||
|
||
test('createPromise() returns an attribute with attribute reject', (t) => { | ||
t.plan(1) | ||
t.ok('reject' in createPromise()) | ||
}) | ||
|
||
test('createPromise() returns an attribute with attribute createPromise', (t) => { | ||
t.plan(1) | ||
t.ok('promise' in createPromise()) | ||
}) | ||
|
||
test('when resolve is called, createPromise attribute is resolved', (t) => { | ||
t.plan(1) | ||
const p = createPromise() | ||
|
||
p.promise | ||
.then(() => { | ||
t.pass() | ||
}) | ||
.catch(() => { | ||
t.fail() | ||
}) | ||
p.resolve() | ||
}) | ||
|
||
test('when reject is called, createPromise attribute is rejected', (t) => { | ||
t.plan(1) | ||
const p = createPromise() | ||
|
||
p.promise | ||
.then(() => { | ||
t.fail() | ||
}) | ||
.catch(() => { | ||
t.pass() | ||
}) | ||
|
||
p.reject() | ||
}) |