I've started to look at using Promises and have begun by putting together a simple function and calling it a few times. I need a sanity check around reject and resolve.
- Is this the correct way to "promisify" a function?
- Is this the correct way to deal with reject and resolve?
Anything I've got totally wrong?
const Redis = require('ioredis'); const redis = new Redis({ port: 6379, host: '127.0.0.1' }); function checkValues(name, section) { return new Promise((resolve, reject) => { redis.multi() .sismember('names', name) .sismember('sections', section) .exec() .then((results) => { if(results[0][1] === 1 && results [1][1] ===1) { reject('Match on both.'); } else if(results[0][1] === 1 || results [1][1] ===1) { reject('Match on one.'); } else { redis.multi() .sadd('names', name) .sadd('sections', section) .exec() .then((results) => { // Lazy assumption of success. resolve('Added as no matches.'); }) // No catch needed as this would be thrown up and caught? } }) .catch((error) => { console.log(error); }); }); } // Call stuff. checkValues('barry', 'green') .then((result) => { // Added as no matches "resolve" message from 'barry', 'green' console.log(result); retutn checkValues('steve', 'blue'); }) .then((result) => { // Added as no matches "resolve" message from 'steve', 'blue' retutn checkValues('steve', 'blue'); }) .then((result) => { // Match on both "reject" message from 'steve', 'blue' console.log(result); }) .catch((error) => { console.log(error); });