source: node_modules/universalify/index.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

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