Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist
View async-to-q.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
var fs = require('fs')
var Q = require('q')
var fs_stat = Q.denodeify(fs.stat)
var fs_readdir = Q.denodeify(fs.readdir)
 
var files = [
'./fixtures/file1',
'./fixtures/file2',
'./fixtures/file3',
'./fixtures/file4'
]
 
var dirs = [
'.',
'../',
'../../'
]
 
// each, eachSeries, and eachLimit are very similar to their map counterparts, only the results are ignored
// order is always preserved, regardless if done in parallel or series
 
function map (arr, func) {
return Q().then(function () {
return arr.map(function (el) { return func(el) })
}).all()
}
 
function mapSeries (arr, func) {
var currentPromise = Q()
var promises = arr.map(function (el) {
return currentPromise = currentPromise.then(function () {
return func(el)
})
})
return Q.all(promises)
}
 
function mapLimit (arr, limit, func) {
var batches = arr.reduce(function (last, next, index) {
if (index % limit == 0) last.push([next])
else last[last.length-1].push(next)
return last
}, [])
 
var currentPromise = Q()
var promises = batches.map(function (batch) {
return currentPromise = currentPromise.then(function () {
var batchPromises = batch.map(function (el) { return func(el) })
return Q.all(batchPromises)
})
})
 
return Q.all(promises)
.then(function (results) {
return Array.prototype.concat.apply([], results) // flatten array
})
}
 
// map(files, fs_stat).then(console.log, console.error)
// function word (file) { throw 'oh no dawg'; return 'word to this file: ' + file }
// map(files, word).then(console.log, function (er) { console.error('oh no', er) })
// mapSeries(files, fs_stat).then(console.log, console.error)
// mapLimit(files, 2, fs_stat).then(console.log, console.error)
 
function filter (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return results[i] })
})
}
 
function filterSeries (arr, func) {
var currentPromise = Q()
var promises = arr.map(function (el) {
return currentPromise = currentPromise.then(function () {
return func(el)
})
})
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return results[i] })
})
}
 
// filter(files, fs_stat).then(console.log, console.error)
// filterSeries(files, fs_stat).then(console.log, console.error)
 
function reject (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return !results[i] })
})
}
 
function rejectSeries (arr, func) {
var currentPromise = Q()
var promises = arr.map(function (el) {
return currentPromise = currentPromise.then(function () {
return func(el)
})
})
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return !results[i] })
})
}
 
// reject(files, fs_stat).then(console.log, console.error)
// rejectSeries(files, fs_stat).then(console.log, console.error)
 
function reduce (arr, initialVal, func) {
var currentPromise = Q(initialVal)
arr.map(function (el) {
return currentPromise = currentPromise.then(function (memo) {
return func(memo, el)
})
})
return currentPromise
}
 
function reduceRight (arr, initialVal, func) {
var currentPromise = Q(initialVal)
arr.reverse().map(function (el) {
return currentPromise = currentPromise.then(function (memo) {
return func(memo, el)
})
})
return currentPromise
}
 
// reduce([1,2,3], 0, function (memo, item) { return Q.delay(100).thenResolve(memo + item) }).then(console.log, console.error)
// reduceRight([1,2,3], 0, function (memo, item) { return Q.delay(100).thenResolve(memo + item) }).then(console.log, console.error)
 
function detect (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return results[i] }).shift()
})
}
 
function detectSeries (arr, func) {
var currentPromise = Q()
var promises = arr.map(function (el) {
return currentPromise = currentPromise.then(function () {
return func(el)
})
})
return Q.all(promises).then(function (results) {
return arr.filter(function (el,i) { return results[i] }).shift() // could be optimized to return first
})
}
 
// detect(files, fs_stat).then(console.log, console.error)
// detectSeries(files, fs_stat).then(console.log, console.error)
 
function sortBy (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return arr.sort(function (a, b) {
return results[arr.indexOf(a)] < results[arr.indexOf(b)] ? -1 : 1
})
})
}
 
// sortBy([2,1,3], function (item) { return Q.delay(100).thenResolve(item) }).then(console.log, console.error)
 
function some (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return results.some(function (el) { return el })
})
}
 
// some([2,1,3], function (item) { return Q.delay(100).thenResolve(item == 1) }).then(console.log, console.error)
 
function every (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return results.every(function (el) { return el })
})
}
 
// every([1,1,1], function (item) { return Q.delay(100).thenResolve(item == 1) }).then(console.log, console.error)
 
function concat (arr, func) {
var promises = arr.map(function (el) { return func(el) })
return Q.all(promises).then(function (results) {
return Array.prototype.concat.apply([], results) // flatten results
})
}
 
function concatSeries (arr, func) {
var currentPromise = Q()
var promises = arr.map(function (el) {
return currentPromise = currentPromise.then(function () {
return func(el)
})
})
return Q.all(promises).then(function (results) {
return Array.prototype.concat.apply([], results) // flatten results
})
}
 
// concat(dirs, fs_readdir).then(console.log, console.error)
// concatSeries(dirs, fs_readdir).then(console.log, console.error)
 
function parallel (funcs) {
var promises = funcs.map(function (func) { return func() })
return Q.all(promises)
}
 
function series (funcs) {
var currentPromise = Q()
var promises = funcs.map(function (func) {
return currentPromise = currentPromise.then(func)
})
return Q.all(promises)
}
 
function parallelLimit (funcs) {
var batches = funcs.reduce(function (last, next, index) {
if (index % limit == 0) last.push([next])
else last[last.length-1].push(next)
return last
}, [])
 
var currentPromise = Q()
var promises = batches.map(function (batch) {
return currentPromise = currentPromise.then(function () {
return Q.all(batch)
})
})
 
return Q.all(promises)
.then(function (results) {
return Array.prototype.concat.apply([], results) // flatten array
})
}
 
function wilst (test, func) {
if (!test()) return Q('wilst')
return func().then(function () {
return wilst(test, func)
})
}
 
function doWilst (func, test) {
return func().then(function () {
if (!test()) return Q('dowilst')
return doWilst(func, test)
})
}
 
// var count = 0
// wilst(function () { return count < 5 }, function () { count++; return Q.delay(100) }).then(console.log, console.error)
// var doCount = 0
// doWilst(function () { doCount++; return Q.delay(100) }, function () { return doCount < 5 }).then(console.log, console.error)
 
function until (test, func) {
if (test()) return Q('until')
return func().then(function () {
return wilst(test, func)
})
}
 
function doUntil (func, test) {
return func().then(function () {
if (test()) return Q('dountil')
return doUntil(func, test)
})
}
 
// var acount = 0
// until(function () { return count > 5 }, function () { acount++; return Q.delay(100) }).then(console.log, console.error)
// var adoCount = 0
// doUntil(function () { adoCount++; return Q.delay(100) }, function () { return adoCount < 5 }).then(console.log, console.error)
 
function forever (func) {
return func().then(function () { return forever(func) })
}
 
// var trigger = 0
// forever(function () {
// if (trigger > 10000) throw "oh no"
// return Q.delay(1)
// }).then(console.log, console.error)
 
function waterfall (funcs) {
return funcs.reduce(Q.when, Q())
}
 
// waterfall([ function () { return fs_stat('./fixtures/file1') }, function (stat) { console.log(stat) } ]).then(console.log, console.error)
 
function compose () {
var funcs = Array.prototype.slice.call(arguments)
return function () {
return funcs.reduce(Q.when, Q.apply(null, arguments))
}
}
 
// function add1 (n) { return Q.delay(1000).thenResolve(n+1) }
// function mul3 (n) { return Q.delay(1000).thenResolve(n*3) }
// var add1mul3 = compose(add1, mul3)
// add1mul3(20).then(console.log, console.error)
 
function applyEach (funcs) {
var args = Array.prototype.slice.call(arguments, 1)
var promises = funcs.map(function (func) { return Q.apply(Q, args).then(func) })
return Q.all(promises)
}
 
function applyEachSeries (funcs) {
var args = Array.prototype.slice.call(arguments, 1)
var currentPromise = Q()
var promises = funcs.map(function (func) {
return currentPromise = currentPromise.then(function () {
return Q.apply(Q, args).then(func)
})
})
return Q.all(promises)
}
 
// function add2 (n) { return Q.delay(1000).thenResolve(n+2) }
// function mul4 (n) { return Q.delay(1000).thenResolve(n*4) }
// applyEach([ add2, mul4 ], 34).then(console.log, console.error)
// applyEachSeries([ add2, mul4 ], 34).then(console.log, console.error)

Thanks for posting this. I'm exploring moving from async to promises with Q right now and this is extremely helpful.

Excellent gist! I've already used it to build a few of my own libraries.

Quick question: any reason why your reduce and reduceRight functions are using Array.map instead of Array.forEach? I created a RSVP.js version of your reduce function using forEach and it seemed to work.

Owner

sheesh I wish github would notify me when someone comments on a gist :) I just got these comments! @twistedstream, i probably was in a map mode, seems forEach would work just as well :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.