this.client = redis.createClient()
const asyncSet = promisify(this.client.set).bind(this.client)
return asyncSet(key, value, "EX", expiry)
jest.mock('redis', () => ({
createClient: jest.fn(() => ({
set: jest.fn((a, b, c, d, callback) => callback(null, true))
}))
}))
Given an function that normally takes a callback as the LAST parameter:
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:
const promise = new Promise()
function(a,b,c,d,callback){
//do some async thing
if(<first callback argument is set>){
promise.reject("ERROR'd")
}else{
promise.resolve("I'm done!")
}
}
return promise