finki-main
main
|
Last change
on this file since 81bc7da was 81bc7da, checked in by Klimentina Efremova <klimentina08642@…>, 3 months ago |
|
Initial commit
|
-
Property mode
set to
100644
|
|
File size:
967 bytes
|
| Rev | Line | |
|---|
| [81bc7da] | 1 | 'use strict'
|
|---|
| 2 |
|
|---|
| 3 | const { promisify } = require('util')
|
|---|
| 4 |
|
|---|
| 5 | const handler = {
|
|---|
| 6 | get: function (target, prop, receiver) {
|
|---|
| 7 | if (typeof target[prop] !== 'function') {
|
|---|
| 8 | return target[prop]
|
|---|
| 9 | }
|
|---|
| 10 | if (target[prop][promisify.custom]) {
|
|---|
| 11 | return function () {
|
|---|
| 12 | return Reflect.get(target, prop, receiver)[promisify.custom].apply(target, arguments)
|
|---|
| 13 | }
|
|---|
| 14 | }
|
|---|
| 15 | return function () {
|
|---|
| 16 | return new Promise((resolve, reject) => {
|
|---|
| 17 | Reflect.get(target, prop, receiver).apply(target, [...arguments, function (err, result) {
|
|---|
| 18 | if (err) {
|
|---|
| 19 | return reject(err)
|
|---|
| 20 | }
|
|---|
| 21 | resolve(result)
|
|---|
| 22 | }])
|
|---|
| 23 | })
|
|---|
| 24 | }
|
|---|
| 25 | }
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | module.exports = function (thingToPromisify) {
|
|---|
| 29 | if (typeof thingToPromisify === 'function') {
|
|---|
| 30 | return promisify(thingToPromisify)
|
|---|
| 31 | }
|
|---|
| 32 | if (typeof thingToPromisify === 'object') {
|
|---|
| 33 | return new Proxy(thingToPromisify, handler)
|
|---|
| 34 | }
|
|---|
| 35 | throw new TypeError('Can only promisify functions or objects')
|
|---|
| 36 | }
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.