From 79b1aed292de5e6d54e367a5fefe53a5e1177adc Mon Sep 17 00:00:00 2001 From: Fabio Crisci Date: Fri, 2 Nov 2018 16:21:24 +0100 Subject: [PATCH] List of cows as promise (#45) Allows users of the library to `await` for it Closes #44 --- lib/cows.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lib/cows.js b/lib/cows.js index 8ebf14d..971ef41 100644 --- a/lib/cows.js +++ b/lib/cows.js @@ -32,10 +32,16 @@ exports.get = function (cow) { } exports.list = function (callback) { - fs.readdir(cowsPath, function (err, files) { - if (err) return callback(err); - - return callback(null, cowNamesFromFiles(files)); + return new Promise(function (resolve, reject) { + fs.readdir(cowsPath, function (err, files) { + if (err) { + reject(err); + callback(err); + } else { + resolve(files); + callback(null, cowNamesFromFiles(files)); + } + }); }); }