asynchronous - Using promises in a javascript filter function and returning it -


i trying return array of session (me) not following javascript's filter function of promise , sending json response.

but doesn't work.

thanks in advance!!

app.get('/explore', (req, res) => {   p.coroutine(function *(){     let         { id: session } = req.session,         followings = yield db.query('select id, username, email users id <> ? order rand() limit 10', [session]),         d = followings.filter(e => {             db.is_following(session, e.id).then(s => s )   // returns boolean         })          res.json(d)   })() }) 

array.prototype.filter synchronous - can't filter array asynchronous filter.

what can do, create array of promises , when of them resolved, return response:

var promises = []; var d = []; followings.foreach(function(e) {   promises.push(     db.is_following(session,e.id).then(function(s) {       //following, push e onto `d`       d.push(e);     }).catch(function() {       //not following, assume, nothing     })   ); });  promise.all(promises).then(function() {   //send response after is_following requests have finished   res.json(d); }); 

Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -