• Login
    • Report A Bug
    • System Information
    • System Tests
    • System Policies
    • Code Documentation
    • Occam Source Code
    • Web Client Source Code
    • About Occam
    • Acknowledgements
    • Guides / Documentation
    • Publications

translation missing: en.flash.saving
translation missing: en.flash.error
  • Bibliography Data
  • Metadata
resource
  • git
    https://github.com/RyanZim/universalify.git Source Repository
    QmVUtjxspzm9d84cnD7uqCsC7cLUHLFb7ZnvFqmjkmr92y
    dc17e0e00fb39c8d52e97ce77e494cdadfa8d19c
      QmVUtjxspzm9d84cnD7uqCsC7cLUHLFb7ZnvFqmjkmr92y
      4342aa98a8801428ac66319c956443c0bfb0ce7e

      • Details
      • Files
      • History
        • Source

          https://github.com/RyanZim/universalify.git

          universalify

          GitHub Workflow Status (branch) Coveralls github branch npm npm

          Make a callback- or promise-based function support both promises and callbacks.

          Uses the native promise implementation.

          Installation

          npm install universalify
          

          API

          universalify.fromCallback(fn)

          Takes a callback-based function to universalify, and returns the universalified function.

          Function must take a callback as the last parameter that will be called with the signature (error, result). universalify does not support calling the callback with three or more arguments, and does not ensure that the callback is only called once.

          function callbackFn (n, cb) {
            setTimeout(() => cb(null, n), 15)
          }
          
          const fn = universalify.fromCallback(callbackFn)
          
          // Works with Promises:
          fn('Hello World!')
          .then(result => console.log(result)) // -> Hello World!
          .catch(error => console.error(error))
          
          // Works with Callbacks:
          fn('Hi!', (error, result) => {
            if (error) return console.error(error)
            console.log(result)
            // -> Hi!
          })
          

          universalify.fromPromise(fn)

          Takes a promise-based function to universalify, and returns the universalified function.

          Function must return a valid JS promise. universalify does not ensure that a valid promise is returned.

          function promiseFn (n) {
            return new Promise(resolve => {
              setTimeout(() => resolve(n), 15)
            })
          }
          
          const fn = universalify.fromPromise(promiseFn)
          
          // Works with Promises:
          fn('Hello World!')
          .then(result => console.log(result)) // -> Hello World!
          .catch(error => console.error(error))
          
          // Works with Callbacks:
          fn('Hi!', (error, result) => {
            if (error) return console.error(error)
            console.log(result)
            // -> Hi!
          })
          

          License

          MIT

          Related

          • maintainer

            npm

          Share

          • URL

            Copy
          • Embed Link

            Copy
          • QR Code

        • JavaScript must be enabled.

          There was an error retrieving this content.

          The content could not be found.

        • JavaScript must be enabled.

          There was an error retrieving this content.

          The content could not be found.

        Confirm
        Do you wish to proceed?

        Confirm message?