|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
816 bytes
|
| Line | |
|---|
| 1 | 'use strict';
|
|---|
| 2 |
|
|---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
|---|
| 4 | value: true
|
|---|
| 5 | });
|
|---|
| 6 | exports.default = awaitify;
|
|---|
| 7 | // conditionally promisify a function.
|
|---|
| 8 | // only return a promise if a callback is omitted
|
|---|
| 9 | function awaitify(asyncFn, arity) {
|
|---|
| 10 | if (!arity) arity = asyncFn.length;
|
|---|
| 11 | if (!arity) throw new Error('arity is undefined');
|
|---|
| 12 | function awaitable(...args) {
|
|---|
| 13 | if (typeof args[arity - 1] === 'function') {
|
|---|
| 14 | return asyncFn.apply(this, args);
|
|---|
| 15 | }
|
|---|
| 16 |
|
|---|
| 17 | return new Promise((resolve, reject) => {
|
|---|
| 18 | args[arity - 1] = (err, ...cbArgs) => {
|
|---|
| 19 | if (err) return reject(err);
|
|---|
| 20 | resolve(cbArgs.length > 1 ? cbArgs : cbArgs[0]);
|
|---|
| 21 | };
|
|---|
| 22 | asyncFn.apply(this, args);
|
|---|
| 23 | });
|
|---|
| 24 | }
|
|---|
| 25 |
|
|---|
| 26 | return awaitable;
|
|---|
| 27 | }
|
|---|
| 28 | module.exports = exports.default; |
|---|
Note:
See
TracBrowser
for help on using the repository browser.