I would like to use util.promisify to promisify custom functions. However, util.promisify works for node-style methods that have an error-first callback as the last argument. How can I adjust my custom functions so that they will work with util.promisify?
Promisifying custom functions with util.promisify
2.7k Views Asked by Nomad_Mio At
1
There are 1 best solutions below
Related Questions in NODE.JS
- Using Puppeteer to scrape a public API only when the data changes
- How to request administrator rights?
- How do I link two models in mongoose?
- Variable inside a Variable, not updating
- Unable to Post Form Data to MongoDB because of picturepath
- Connection terminated unexpectedly while performing multi row insert using pg-promise
- Processing multiple forms in nodejs and postgresql
- Node.js Server + Socket.IO + Android Mobile Applicatoin XHR Polling Error...?
- How to change the Font Weight of a SelectValue component in React when a SelectItem is selected?
- My unban and ban commands arent showing when i put the slash
- how to make read only file/directory in Mac writable
- How can I outsource worker processes within a for loop?
- Get remote MKV file metadata using nodejs
- Adding google-profanity-words to web page
- Products aren't displayed after fetching data from mysql db (node.js & express)
Related Questions in NODE-PROMISIFY
- Javascript - Convert function that have a least two callbacks style to promise style
- How to promisify a mysql pool connection in Node js?
- Is this valid syntax for node/promisify async function?
- How does promisify know which variable to return?
- NodeJS: Await Skips Catch Block and Exits Function. Why?
- How to mock a promisified and binded function in JEST?
- How do I use async/await in a mocha before hook?
- throw new TypeError("expecting a function but got " + util.classString(fn)); TypeError: expecting a function but got [object Undefined]
- subjectify methods with callbacks
- Why would promisify cause a loop to take massively longer?
- Create register: TypeError: Right-hand side of 'instanceof' is not callable Express and Promisify
- Calling a function after everything else is finished
- Promisify in redis client violates eslint rules
- Why don't my promisify and jwt return values or errors?
- Returning Promise { undefined } in NodeJS when making a function call
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
My first choice would be to just add a new API that returns a promise - that wraps your own API inside a new Promise constructor. Then, you have a documented, promise-returning API that is obvious how to use and requires no extra steps by the client to use it.
But, if you really want to make something compatible with
util.promisify(), read on...If you had shown the calling convention for your function that you want
util.promisify()to work with, we could have offered a custom implementation specifically for that code, but since you didn't here's an example implementation below.The general concept is described here in the doc, though I would not exactly say the doc is very clear about how things work. Here's an example.
Suppose you have a timer function that takes three arguments in this order:
where
tis the time in ms for the timer andvis the value it will pass to the callback when the timer fires. And, it passes the callback two valuescallback(v, err)in that order. And, yes, this particular timer can sometimes communicate back errors.Note, specifically that the callback is passed as the first argument and the callback itself gets
erras the second argument, both of which are obviously incompatible with the default implementation ofutil.promisify()(for purposes of this demonstration) as they don't match the nodejs asynchronous calling convention.Here's an example usage of this timer (non-promisified) function that does NOT follow the nodejs calling convention:
To make it compatible with
util.promisify(), we can create a custom promisify behavior like this:The general concept is that when you pass a function reference to
util.promisify(fn), it looks forfn[util.promisify.custom]and, if it exists, it will use that promisify implementation instead of its default one. If you're wondering whatutil.promisify.customis, it's a symbol defined by theutillibrary - just a unique property name that you can assign to your own function as a property in order to identify that you have implemented a custom promisify capability. Since symbols are unique within any nodejs implementation, adding that property to your function can't interfere with any other properties.Which can then be used like this by other callers:
If you want to see the nodejs implementation for
util.promisify(), you can see it here in the actual util.js code.