javascript - Bluebird nested promises with each/spread -


i'm facing issue while using bluebird promises. i'm using coffeescript javascript answers welcome :)

here i'm trying :

code example

promise = require 'bluebird' model = promise.promisifyall(require '[...]') # mongoose model promisified  getopts = () -> [...] # whatever  promise.each [1..3], (number) ->   opts = getopts(number)   return model.count(opts).exec (err, count) ->     return "the count #{count}" .spread () ->   console.log json.stringify arguments   result = arguments.join(',')   [...] 

explanations

i want run same function 1, 2, 3 (sequentially), use bluebird's .each function. in function, need count database. i'm using mongoose bluebird's promisifyall function return promise , make sure .each waits until each query done go next.

then, gather result of each query. i'm using bluebird's spread gather return values of .each. however, doesn't contain return value of nested promise. arguments value :

{   "0": 1,   "1": 2,   "2": 3 } 

any ideas ?

thanks

edit

looks bluebird's .each doesn't go .spread. i'm investigating on issue.

edit 2

(thanks bergi)

i found solution : first build array of functions. call bluebird's .all on array, , .spread.

edit 3

ok, tried .map instead of .each , works great also.

looks bluebird's .each doesn't go .spread

yes indeed. each return promise array, if want use array use then:

promise.each [1..3], (number) ->   opts = getopts(number)   model.countasync(opts).then (count) ->     "the count #{count}" .then (args) ->   console.log json.stringify args   result = args.join(',')   […] 

spread callback functions want use individual variable (function parameter) every array element, .spread (firstresult, secondresult, thirdresult) -> ….

also, arguments array-like object, not true array, that's why odd json representation , why .join call throw exception.

however, doesn't contain return value of nested promise

yes, each thought side effects originally, , instead of collecting results returns original input. change version 3.0. until then, can use map. see bluebird: getting results of each() further details.


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -