andy246 Posted June 27, 2015 Share Posted June 27, 2015 This is a question I got asked in an interview. I know the array would just return zeros since this is asynchronous, but why does this happen, and how can you fix it so that the results array inserts the appropriate data? Question: Suppose findData is a function that takes a query object and returns a promise for the result of the query. Suppose also that someRandomArrayOfQueries is an array of query objects. Explain what would be printed by the following code and why: function runMultipleQueries(queries) { var results = []; queries.forEach(doQuery); return results; function doQuery(query) { findData(query) .then(results.push.bind(results)); } }function log(value) { console.log(value);}runMultipleQueries(someRandomArrayOfQueries).forEach(log); Link to comment Share on other sites More sharing options...
davej Posted June 28, 2015 Share Posted June 28, 2015 I am not yet familiar enough with promises to comment. In fact I just bought this book a few days ago. http://shop.oreilly.com/product/0636920032151.do Link to comment Share on other sites More sharing options...
Matej Posted July 8, 2015 Share Posted July 8, 2015 I am not yet familiar enough with promises to comment. In fact I just bought this book a few days ago. http://shop.oreilly.com/product/0636920032151.do Hola, have you taken a look at it yet? Ihave ordered that book for promises , but does it include fetch api which is based on promises? Link to comment Share on other sites More sharing options...
Ingolme Posted July 8, 2015 Share Posted July 8, 2015 The only thing that throws me off is this: results.push.bind(results) The push method is used to add an element to an array. As far as I know, it doesn't have a bind() method. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2015 Share Posted July 8, 2015 It's just a way to set this inside a function. Although I really doubt it's necessary there, it should already be set to results if using push.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind Link to comment Share on other sites More sharing options...
Ingolme Posted July 8, 2015 Share Posted July 8, 2015 (edited) OK. What seems to be missing is arguments for the push method. Oh, actually. The promise passes the parameter. I guess the solution for the asynchronous problem would be to have a callback when all the queries were done. Edited July 8, 2015 by Ingolme Link to comment Share on other sites More sharing options...
justsomeguy Posted July 8, 2015 Share Posted July 8, 2015 So I guess that could be results.push instead of the bind, although maybe that actually is necessary to get the right scope. Link to comment Share on other sites More sharing options...
davej Posted July 10, 2015 Share Posted July 10, 2015 This is really going to be a huge learning curve. The changes from ES5 are massive. http://www.ecma-international.org/ecma-262/6.0/ Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now