javascript - Waiting for 2 promises to come back to use simultaneously in a function -


first-off: hope question has not been asked , answered elsewhere... i've been looking , web on , can't seem find (maybe it's not possible).

basically, i'm trying build db (in case mongo) info webapi (the crypto exchange kraken). objective build collections various objects requests.

here gitub repo: mongonode

here's current code (i'm using simple callbacks , request @ moment: chaining everything):

// external modules var request = require('request'); var yargs = require('yargs'); var _ = require('underscore'); var async = require('async'); var mongoclient = require('mongodb').mongoclient,   co = require('co'),   assert = require('assert');  // own modules  // variables var tickerarr= []  // generator function connection co(function*() {   // connection url   var url = 'mongodb://localhost:27017/cryptodb';   // use connect method connect server   var db = yield mongoclient.connect(url);    // inside db connection   console.log('connection , running...');    // getting list of tickers usd , eur exclusively   request({     url:'https://api.kraken.com/0/public/assetpairs',     json: true   }, (error, response, body) => {     var jsonobject = body;     _.map(jsonobject, function(content) {       _.map(content, function(data) {         if(data.altname.indexof('eur') !== -1 || data.altname.indexof('usd') !== -1)           tickerarr.push(data.altname);       });     });     // getting ticker info each usd && eur ticker available     async.foreach(tickerarr, (item) => {       request({         url:`https://api.kraken.com/0/public/ticker?pair=${item}`,         json: true       }, (error, response, body) => {               db.collection('assets').insertone(                 body.result,                 request({                       url:'https://api.kraken.com/0/public/time',                       json: true                     }, (error, response, body) => {                         return body.result;                     })                   );               console.log('asset added!', body.result);       });     });   });   // closing db connection   // db.close();   }).catch(function(err) {   console.log(err.stack); }); 

i've trying use promises chaining 'em can't seem find way work. here's promise playground:

var somepromise1 = new promise((resolve, reject) => {   resolve('hey! worked!'); });  var somepromise2 = new promise((resolve, reject) => {   resolve('hey! worked second time!'); });  somepromise1.then(somepromise2.then((message1, message2) => {   console.log('success', message1, message2); }) ); 

and here's print in console when executed:

$success hey! worked second time! undefined

thanks in advance , hope question hasn't been answered million times!

var somepromise1 = new promise((resolve, reject) => {    resolve('hey! worked!');  });    var somepromise2 = new promise((resolve, reject) => {    resolve('hey! worked second time!');  });    somepromise1.then((message1) => somepromise2.then((message2) => {    console.log('success', message1, message2);  })  );

promises quite convoluted though. async/await makes things lot clearer:

var somepromise1 = new promise((resolve, reject) => {    resolve('hey! worked!');  });    var somepromise2 = new promise((resolve, reject) => {    resolve('hey! worked second time!');  });      async function exec() {    const message1 = await somepromise1    const message2 = await somepromise2    console.log('success', message1, message2);  }    exec()


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -