Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Given an api that looks like this:
- ```javascript
- this.client = redis.createClient()
- const asyncSet = promisify(this.client.set).bind(this.client)
- return asyncSet(key, value, "EX", expiry)
- ```
- ### The correct corresponding mock looks like:
- ```javascript
- jest.mock('redis', () => ({
- createClient: jest.fn(() => ({
- set: jest.fn((a, b, c, d, callback) => callback(null, true))
- }))
- }))
- ```
- ## How promisify works:
- Given an function that normally takes a callback as the LAST parameter:
- ```javascript
- function(a,b,c,d,callback){
- //do some async thing
- callback(null, "I'm done!") //success response
- OR
- callback("ERROR'd", null) //error response
- }
- ```
- So the overly simplified "promisify'd" code might look like:
- ```javascript
- const promise = new Promise()
- function(a,b,c,d,callback){
- //do some async thing
- if(<first callback argument is set>){
- promise.resolve("I'm done!")
- }else{
- promise.reject("ERROR'd")
- }
- }
- return promise
- ```
Add Comment
Please, Sign In to add comment