Skip to content

Instantly share code, notes, and snippets.

@jacob-faber
Last active December 4, 2015 05:55
Show Gist options
  • Save jacob-faber/01f5a0b0e061ad798dfd to your computer and use it in GitHub Desktop.
Save jacob-faber/01f5a0b0e061ad798dfd to your computer and use it in GitHub Desktop.
Working with asynchronous functions
var db = {
// Async function 1
findUsers: function (callback) {
setTimeout(function () {
console.log('findUsers()');
callback([
{name: 'Jacob', age: 22},
{name: 'Michaela', age: 21}
])
}, 1000)
},
// Async function 2
findPosts: function (callback) {
setTimeout(function () {
console.log('findPosts()');
callback([
{title: 'First post'},
{title: 'Last post'}
])
}, 2000)
}
};
// ---------------------------------------------------------------- //
// Async functions run in PARALLEL, right version
// Both findPosts() and findUsers() start `at the same` time and
// on their finish, we decrement `counter` and if it's `0`, we know that
// both of them finished and we have our values in `parallelRight` object
// Time: 2000 ms
var parallelRight = {};
var counter = 2; // We have 2 async functions
function isFinished() {
if (--counter) return; // Decrement by 1, because when this runs, async function invoked callback
// Both async functions finished
console.log(parallelRight);
console.timeEnd('Async Parallel Right');
}
console.time('Async Parallel Right');
db.findPosts(function (posts) {
parallelRight.posts = posts; // callback body executed after 1000ms
isFinished()
});
db.findUsers(function (users) {
parallelRight.users = users; // callback body executed after 2000ms
isFinished()
});
var db = {
// Async function 1
findUsers: function (callback) {
setTimeout(function () {
console.log('findUsers()');
callback([
{name: 'Jacob', age: 22},
{name: 'Michaela', age: 21}
])
}, 1000)
},
// Async function 2
findPosts: function (callback) {
setTimeout(function () {
console.log('findPosts()');
callback([
{title: 'First post'},
{title: 'Last post'}
])
}, 2000)
}
};
// ---------------------------------------------------------------- //
// Async functions run in PARALLEL, wrong version
// Both findPosts() and findUsers() start `at the same` time, but
// `console.log(parallelWrong)` prints before they have chance to finish
// Time: 2000 ms
var parallelWrong = {};
console.time('Async Parallel Wrong');
db.findPosts(function (posts) {
parallelWrong.posts = posts; // callback body executed after 1000ms
});
db.findUsers(function (users) {
parallelWrong.users = users; // callback body executed after 2000ms
});
console.log(parallelWrong); // Prints {}
console.timeEnd('Async Parallel Wrong');
var db = {
// Async function 1
findUsers: function (callback) {
setTimeout(function () {
console.log('findUsers()');
callback([
{name: 'Jacob', age: 22},
{name: 'Michaela', age: 21}
])
}, 1000)
},
// Async function 2
findPosts: function (callback) {
setTimeout(function () {
console.log('findPosts()');
callback([
{title: 'First post'},
{title: 'Last post'}
])
}, 2000)
}
};
// ---------------------------------------------------------------- //
// Async functions run in SERIES
// First runs findPosts(), when it's callback is executed, findUsers() is invoked
// Time: 3000 ms (actually always a bit more) TODO reference
var series = {};
console.time('Async Series');
db.findPosts(function (posts) {
db.findUsers(function (users) {
series.posts = posts;
series.users = users;
console.log(series);
console.timeEnd('Async Series');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment