-2

I have a function which i have promisifed using 'es6-promisify'. I want to replicate the functionality using 'q'.

Here is a sample code which i'm using:

const promisify = require('es6-promisify')

function asyncFunction (done) {
  console.time('asyncFunction')
  setTimeout(() => {
    console.timeEnd('asyncFunction')
    done()
  }, 500)
}

const asyncFunctionPromise = promisify(asyncFunction)

Also, lets say I get an error while running a said function which I have promisified. How do I handle those errors?

Sarvesh Redkar
  • 113
  • 1
  • 2
  • 11

1 Answers1

-1

I have been messing around with the 'q' library for quite some time today and finally think I have figured out a way.

Here's what I did and it was quite simple, now that i think about it.

const q = require('q')

function asyncFunction (done) {
  console.time('asyncFunction')
  setTimeout(() => {
    console.timeEnd('asyncFunction')
    done()
  }, 500)
}

const asyncFunctionPromise = q.denodeify(asyncFunction)

Just instead of

const promisify = require('es6-promisify')

use

const q = require('q')

and replace

const asyncFunctionPromise = promisify(asyncFunction)

with

const asyncFunctionPromise = q.denodeify(asyncFunction)

Also regarding the error handling: Handle the errors within the function. the Promise with automatically handle those errors.

PS: You can also use q.deferred to create Promises. It has similar syntax to jQuery.deferred.

Hope this helps someone with issues regarding broken Promises (Pun Intended.)

Sarvesh Redkar
  • 113
  • 1
  • 2
  • 11