标签云

微信群

扫码加入我们

WeChat QR Code

I've done hours of research on asynchronous programming, but I just can't seem to grasp this one single concept within Node, so I was wondering if someone here could help me.I've written the following code sample to return / output a simple string which is a concatenation of strings from an object:var itemCollection = {item1 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }],item2 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }],item3 : [{ foo : "bar" }, { foo : "bar" }, { foo : "bar" }]}var aString = "";for(item in itemCollection){for (var i = 0; i < itemCollection[item].length; i++) {var anItem = itemCollection[item][i];//someFunctionThatDoesALongIOOperation takes an item as a param, plus a callback.someFunctionThatDoesALongIOOperation(anItem, function(dataBackFromThisFunction){// Do something with the data returned from this functionaString += dataBackFromThisFunction.dataToAppend;});};}console.log(aString);So from what I understand, languages other than Javascript would run someFunctionThatDoesALongIOOperation synchronously and the script would run in a 'blocking mode'. This would mean that the value aString would get returned / outputted with its correct value.However, as Node runs asynchronously, code can continue to run at anytime and tasks may not complete in order. This is because of the way the event loop works in Node. I think I get this.So this is where my question comes in. If I wanted the value aString to be returned / outputted with its correct value like it would in other languages, what would I need to do to the loops within my code example? Or to put my question in more technical words: What is the correct approach for making aString return the expected result, so that IO operations (which take alonger amount of time to run) aren't completed after the script has finished executing when aString has already been returned?I hope my question makes sense, if it doesn't, please let me know and I will make edits where appropriate.Thank you


Are you looking to make the code async?

2019年04月18日08分45秒

languages other than Javascript would run someFunctionThatDoesALongIOOperation synchronously - not necesarily - depends on how the function is coded

2019年04月18日08分45秒

Node.js, by defualt is not just somehow async, but the API is, and it is encouraged that you write you code to be async

2019年04月18日08分45秒

Why map and reduce instead of just each or parallel?

2019年04月18日08分45秒

reduce is executed in series and is probably more semantic in the particular application. map is picked since the callback is only called when everything is finished and can be reduced separately afterwards. each executes everything in parallel, so the order of the result still isn't guaranteed and parallel can be equivalent to map, but requires a separate function for each element in the array (and still needs to be reduced manually afterwards).

2019年04月19日08分45秒

Aye, sorry meant series not each. But yeah I see what you're suggesting.

2019年04月18日08分45秒

series would work, but like parallel, is intended for control flow of many different async operations, rather than applying a same operation on many things.

2019年04月18日08分45秒