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
Changes from 1 commit
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
29 changes: 29 additions & 0 deletions lib/create-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
'use strict'

/**
* @typedef PromiseObject
* @property {Promise} promise
* @property {PromiseConstructor["resolve"]} resolve
* @property {PromiseConstructor["reject"]} reject
*/

/**
* @returns {PromiseObject}
*/
function createPromise () {
/**
* @type {PromiseObject}
*/
const obj = {}

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
@@ -4,6 +4,7 @@ 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')

// this symbol is assigned by fastify-plugin
@@ -28,17 +29,6 @@ function getName (func, optsOrFunc) {
return func.toString().split('\n').slice(0, 2).map(s => s.trim()).join(' -- ')
}

function promise () {
const obj = {}

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

return obj
}

function Plugin (parent, func, optsOrFunc, isAfter, timeout) {
this.started = false
this.func = func
@@ -165,7 +155,7 @@ Plugin.prototype.loadedSoFar = function () {
let res

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

if (!this.server) {
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()
})