Last active
November 10, 2018 01:02
-
-
Save daluu/6e01b80493168a85caf5 to your computer and use it in GitHub Desktop.
Dealing with return values for asynchronous calls in javascript (when you're accustomed to synchronous code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://blog.cloudboost.io/execute-asynchronous-tasks-in-series-942b74697f9c | |
https://blog.scottlogic.com/2017/09/14/asynchronous-recursion.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function asyncWrapper(args,callback){ | |
some_async_method_or_event_block('data', function(asyncResult) { | |
//normally for simple async use, you deal with the "result" here | |
//but we'll defer to the caller to deal with the result ~ synchronously | |
//by returning or passing back the result to the caller | |
callback(asyncResult); | |
}); | |
//some code here that triggers the async event block above if it was an event handler rather than a method | |
//this code may use the args supplied by the wrapper. | |
//no code needed here otherwise if the above block was a method | |
} | |
//call the async wrapper to run the async code in a synchronous-like way that you're more familiar with, this way... | |
asyncWrapper(someData, function(syncLikeResult) { | |
//now deal with the result as you like | |
//code here would be like code that is run after the asyncWrapper call in a synchronous scenario | |
console.log(syncLikeResult); | |
}); | |
//the above asyncWrapper call approximately simulates the synchronous behavior & style one is accustomed to like this: | |
//var syncLikeResult = asyncWrapper(someData); | |
//console.log(syncLikeResult); | |
//where the asyncWrapper implementation in synchronous style would have looked like this: | |
/* | |
function asyncWrapper(args){ | |
//do something with args, synchronously | |
return result; | |
} | |
*/ | |
//except that ^^ above is not possible with asynchronous code, | |
//so you do the best alternative via the uncommented code block above | |
/* additional tip on handling callbacks where you might have to iterate an array | |
or hash/dictionary to do more callbacks and sync the result back | |
with the main code/callback can be found in the original solution here: | |
https://wordpress.com/read/blogs/39441502/posts/58 | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* TODO, I haven't really dealt with code that uses promises yet... | |
to be able to showcase how to work it in a synchronous way/feel | |
but for now, good to read up on this: https://www.promisejs.org | |
or the tips/samples under | |
https://blog.cloudboost.io/execute-asynchronous-tasks-in-series-942b74697f9c | |
https://github.com/NodeRedis/node_redis | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment