About what exactly your problem has already written in the neighboring answers. I will give a solution to the problem using Promises :
function a() { return b().then(function(timeout) { return new Promise(function(resolve) { setTimeout(resolve, timeout); }); }).then(function() { console.log(3); }); } function b() { console.log(1); return (new Promise(function(resolve) { setTimeout(resolve, 5000); })).then(function() { console.log(2); return 2000; }); } a();
And here is a working example on JSFiddle.
If the initial set of functions is not essential, then everything can be made somewhat simpler:
(new Promise(function(resolve) { // Получаем данные с сервера console.log(1); setTimeout(function() { resolve(2000); }, 5000); })).then(function(timeout) { // Обрабатываем данные синхронно console.log(2); return timeout + 1; }).then(function(timeout) { // Обрабатываем данные аснихронно. return new Promise(function(resolve) { setTimeout(function() { resolve('Foo-Bar-Baz'); }, timeout); }); }).then(function(data) { // Выводим данные console.log(3); console.log(data); });