Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

extract create-promise #218

Merged
merged 3 commits into from
Jun 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions lib/create-promise.js
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
}
14 changes: 2 additions & 12 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,10 @@ const fastq = require('fastq')
const EE = require('events').EventEmitter
const inherits = require('util').inherits
const { debug } = require('./lib/debug')
const { createPromise } = require('./lib/create-promise')
const { AVV_ERR_READY_TIMEOUT } = require('./lib/errors')
const { getPluginName } = require('./lib/get-plugin-name')

function promise () {
const obj = {}

obj.promise = new Promise((resolve, reject) => {
obj.resolve = resolve
obj.reject = reject
})

return obj
}

function Plugin (parent, func, options, isAfter, timeout) {
this.started = false
this.func = func
Expand Down Expand Up @@ -144,7 +134,7 @@ Plugin.prototype.loadedSoFar = function () {
let res

if (!this._promise) {
this._promise = promise()
this._promise = createPromise()
res = this._promise.promise

if (!this.server) {
Expand Down
55 changes: 55 additions & 0 deletions test/lib/create-promise.test.js
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()
})