src/interfaces/thenable.js

Maintainability

80.92

Lines of code

87

Created with Raphaël 2.1.002550751002019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-1

2019-10-30
Maintainability: 80.92

Created with Raphaël 2.1.0022.54567.5902019-3-122018-12-32018-8-22018-5-92018-4-32018-2-282017-12-212017-11-1

2019-10-30
Lines of Code: 87

Difficulty

6.89

Estimated Errors

0.09

Function weight

By Complexity

Created with Raphaël 2.1.0_gpfPromisify2

By SLOC

Created with Raphaël 2.1.0_gpfPromisify6
1
/**
2
 * @file IThenable interface
3
 * @since 0.2.2
4
 */
5
/*#ifndef(UMD)*/
6
"use strict";
7
/*global _gpfDefineInterface*/ // Internal interface definition helper
8
/*global _gpfSyncReadSourceJSON*/ // Reads a source json file (only in source mode)
9
/*exported _gpfIThenable*/ // gpf.interfaces.IThenable
10
/*exported _gpfPromisify*/ // Converts any value into a Promise
11
/*exported _gpfPromisifyDefined*/ // Converts any value but undefined into a Promise
12
/*#endif*/
13
 
14
/**
15
 * The Thenable interface helps identifying Promise object through the
16
 * [then](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) method
17
 *
18
 * @interface gpf.interfaces.IThenable
19
 * @since 0.2.2
20
 */
21
 
22
/**
23
 * The then() method returns a Promise. It takes up to two arguments: callback functions for the success and failure
24
 * cases of the Promise
25
 *
26
 * @method gpf.interfaces.IThenable#then
27
 * @param {Function} onFulfilled called if the Promise is fulfilled
28
 * @param {Function} [onRejected] called if the Promise is rejected
29
 * @return {Promise} Promise
30
 * @since 0.2.2
31
 */
32
 
33
/**
34
 * IThenable interface specifier
35
 *
36
 * @type {gpf.interfaces.IThenable}
37
 * @since 0.2.2
38
 */
39
var _gpfIThenable = _gpfDefineInterface("Thenable",
40
    _gpfSyncReadSourceJSON("interfaces/thenable.json"));
41
 
42
/**
43
 * Converts any value into a promise.
44
 * If the value implements {@link gpf.interfaces.IThenable}, it is considered as a promise.
45
 *
46
 * @param {*} value Value to convert
47
 * @return {Promise<*>} Promisified version of the value
48
 * @since 0.2.2
49
 */
50
function _gpfPromisify (value) {
51
    if (gpf.interfaces.isImplementedBy(gpf.interfaces.IThenable, value)) {
52
        return value;
53
    }
54
    return Promise.resolve(value);
55
}
56
 
57
/**
58
 * Converts value into a promise if not undefined.
59
 * If the value implements {@link gpf.interfaces.IThenable}, it is considered as a promise.
60
 *
61
 * @param {*} value Value to convert
62
 * @return {Promise<*>|undefined} Promisified version of the value or undefined
63
 * @since 0.2.2
64
 */
65
function _gpfPromisifyDefined (value) {
66
    if (undefined !== value) {
67
        return _gpfPromisify(value);
68
    }
69
}
70
 
71
/**
72
 * @gpf:sameas _gpfPromisify
73
 * @since 0.2.2
74
 * @deprecated since version 0.2.6, use
75
 * [Promise.resolve](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)
76
 * instead
77
 */
78
gpf.promisify = _gpfPromisify;
79
 
80
/**
81
 * @gpf:sameas _gpfPromisifyDefined
82
 * @since 0.2.2
83
 * @deprecated since version 0.2.6, use
84
 * [Promise.resolve](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve)
85
 * combined with a condition instead
86
 */
87
gpf.promisifyDefined = _gpfPromisifyDefined;