source: trip-planner-front/node_modules/universalify/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 777 bytes
RevLine 
[6a3a178]1'use strict'
2
3exports.fromCallback = function (fn) {
4 return Object.defineProperty(function () {
5 if (typeof arguments[arguments.length - 1] === 'function') fn.apply(this, arguments)
6 else {
7 return new Promise((resolve, reject) => {
8 arguments[arguments.length] = (err, res) => {
9 if (err) return reject(err)
10 resolve(res)
11 }
12 arguments.length++
13 fn.apply(this, arguments)
14 })
15 }
16 }, 'name', { value: fn.name })
17}
18
19exports.fromPromise = function (fn) {
20 return Object.defineProperty(function () {
21 const cb = arguments[arguments.length - 1]
22 if (typeof cb !== 'function') return fn.apply(this, arguments)
23 else fn.apply(this, arguments).then(r => cb(null, r), cb)
24 }, 'name', { value: fn.name })
25}
Note: See TracBrowser for help on using the repository browser.