summaryrefslogtreecommitdiff
path: root/lib/internal/wrapAsync.js
blob: aee0275044e5e4798c17d61407cacae44fbbc56d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import identity from 'lodash/identity';
import asyncify from '../asyncify';

var supportsSymbol = typeof Symbol === 'function';

function supportsAsync() {
    var supported;
    try {
        /* eslint no-eval: 0 */
        supported = isAsync(eval('(async function () {})'));
    } catch (e) {
        supported = false;
    }
    return supported;
}

function isAsync(fn) {
    return supportsSymbol && fn[Symbol.toStringTag] === 'AsyncFunction';
}

function wrapAsync(asyncFn) {
    return isAsync(asyncFn) ? asyncify(asyncFn) : asyncFn;
}

export default supportsAsync() ? wrapAsync : identity;

export { supportsAsync, isAsync };