I don't understand how it must working :(( (fetch, forEach, etc.)

Sorry for this. Ok, in first fetch i get list of friends. In second i tried get one picture for every friend, and build new array whit adding new lines (first_name and photo_200). But it looks weird. Program shows some of friend (random count) and in random order O_O. And frnd variable work only in fetch, when i tried use it in other func i get ‘undefined’. Very sorry for my english, but this problem will kill me soon :smiley:

var Observable = require('FuseJS/Observable');
var sphoto = Observable();
var token = Observable("");
var arr = Observable();
var frnd = Observable();
var friends = Observable();
var k = 0;
this.Parameter.onValueChanged(function(args) {
	token.value =  args;
	getAllSave();
});




function getAllSave() {
	var url = "https://api.vk.com/method/friends.get?";
	url += "access_token=" + token.value + "&fields=photo_200&order=hints";
	fetch(url) //i get list of friends
	.then(function(res) {
		return res.json();
	})
	.then(function(resJson) {
		frnd.value = resJson;
		console.log("frnd.value = " + JSON.stringify(frnd.value));
		url = "https://api.vk.com/method/photos.get?";
		frnd.value.response.forEach(function(r) {
		url += "access_token=" + token.value + "&owner_id=" + r.uid + "&album_id=saved&rev=1&count=10&extended=1";
		fetch(url) 
		.then(function(res) {
			return res.json();
		})
		.then(function(resJson) {
                        k++;
			console.log("it is " + k + " iteration, name is " + r.first_name)
			for (var j = 0; j < 1; j++) {
				resJson.response[j].first_name = r.first_name;
				resJson.response[j].photo_200 = r.photo_200;
			    friends.add(resJson.response[j]);
		    }
		})
		.catch(function(err) {
			console.log("err" + err);
		})
		url = "https://api.vk.com/method/photos.get?";
		})
	})

}
module.exports = {
	sphoto: friends
};

LOG:

LOG: frnd.value = {"response":[{"uid":298682753,"first_name":"Vladislava","last_name":"Tsebinoga","photo_200":"https://pp.vk.me/c637818/v637818753/eed3/wj-jdLzTI5k.jpg","online":0,"user_id":298682753},{"uid":41901095,"first_name":"Денис","last_name":"Миргородский","photo_200":"https://pp.vk.me/c636817/v636817095/333cd/OWMnr9iH5sY.jpg","online":0,"user_id":41901095},{"uid":93860755,"first_name":"Андрей","last_name":"Иногда","photo_200":"https://pp.vk.me/c836326/v836326755/8f9b/88QjMd_AtRE.jpg","online":1,"user_id":93860755},{"uid":126230397,"first_name":"Роман","last_name":"Клемешов","photo_200":"https://pp.vk.me/c633417/v633417397/393de/GbK3fpKKsDY.jpg","online":0,"user_id":126230397},{"uid":243176804,"first_name":"Миша","last_name":"Шаповалов","photo_200":"https://pp.vk.me/c636525/v636525804/3ff99/qRbcUw9axS0.jpg","online":1,"user_id":243176804},{"uid":128228327,"first_name":"Назар","last_name":"Кулакевич","photo_200":"https://pp.vk.me/c626231/v626231327/13fd1/Nsq1195lj4s.jpg","online":1,"user_id":128228327},{"uid":120042880,"first_name":"Алёна","last_name":"Ивлева","photo_200":"https://pp.vk.me/c626723/v626723880/3afdd/VOHRfD38mq8.jpg","online":1,"user_id":120042880},{"uid":142372858,"first_name":"Влад","last_name":"Коваленко","photo_200":"https://pp.vk.me/c626418/v626418858/2c633/iIj-Oq25RYA.jpg","online":1,"user_id":142372858},{"uid":146946054,"first_name":"Артём","last_name":"Сутырин","photo_200":"https://pp.vk.me/c626717/v626717054/3447a/dNQlDN7To_s.jpg","online":0,"user_id":146946054},{"uid":276847267,"first_name":"Даша","last_name":"Стреляная","photo_200":"https://pp.vk.me/c638728/v638728267/aa6d/X7GOgzzG9i0.jpg","online":1,"user_id":276847267},{"uid":138082042,"first_name":"Влад","last_name":"Харьков","photo_200":"https://pp.vk.me/c623321/v623321042/5a58e/GjLgo16nEiE.jpg","online":1,"user_id":138082042},{"uid":33516486,"first_name":"Андрей","last_name":"Бей","photo_200":"https://pp.vk.me/c626125/v626125486/2fba5/Mb_DMsjWIR4.jpg","online":1,"user_id":33516486},{"uid":57586982,"first_name":"Владимир","last_name":"Дзикевич","photo_200":"https://pp.vk.me/c626317/v626317982/2fbd4/degrENqyXNQ.jpg","online":1,"user_id":57586982},{"uid":186168488,"first_name":"Максим","last_name":"Долгий","photo_200":"https://pp.vk.me/c636824/v636824488/1090b/sPsvR4Nr2u4.jpg","online":0,"user_id":186168488},{"uid":289655570,"first_name":"Евгений","last_name":"Пархомец","photo_200":"https://pp.vk.me/c625419/v625419570/51fd7/AWbiEULSaaI.jpg","online":0,"user_id":289655570},{"uid":233800425,"first_name":"Вова","last_name":"Заика","photo_200":"https://pp.vk.me/c630627/v630627425/367e9/e5fv3lSAlko.jpg","online":0,"user_id":233800425},{"uid":167757294,"first_name":"Alexandra","last_name":"Sourire","photo_200":"https://pp.vk.me/c630330/v630330294/438c7/ejFCJAmMB-E.jpg","online":0,"user_id":167757294},{"uid":29308878,"first_name":"Алексей","last_name":"Федоренко","photo_200":"https://pp.vk.me/c837336/v837336878/a82a/O_4O5UzWBh4.jpg","online":1,"user_id":29308878},{"uid":56755871,"first_name":"Дарья","last_name":"Полтавская","photo_200":"https://pp.vk.me/c626429/v626429871/d497/mteCwLDoRyU.jpg","online":1,"user_id":56755871},{"uid":102668297,"first_name":"Денис","last_name":"Бова","photo_200":"https://pp.vk.me/c626518/v626518297/3aac2/acht_PAFDD4.jpg","online":1,"user_id":102668297},{"uid":326436594,"first_name":"Анна","last_name":"Волкова","photo_200":"https://pp.vk.me/c637820/v637820594/1d76c/cGqgwJdprjM.jpg","online":0,"user_id":326436594},{"uid":320358622,"first_name":"User","last_name":"Off-Line","photo_200":"https://pp.vk.me/c627222/v627222622/14690/GchJU-SmYmA.jpg","online":0,"user_id":320358622},{"uid":292133500,"first_name":"Аня","last_name":"Власова","photo_200":"https://pp.vk.me/c637119/v637119500/4a96/YqpbPalLG4Y.jpg","online":0,"user_id":292133500},{"uid":318394157,"first_name":"Руслан","last_name":"Надточий","photo_200":"https://pp.vk.me/c637331/v637331157/1aa87/j7uC6FH_PoE.jpg","online":0,"user_id":318394157},{"uid":16537593,"first_name":"Кирилл","last_name":"Кириченко","photo_200":"https://pp.vk.me/c637530/v637530593/16e90/5Q8qBkLEGvQ.jpg","online":0,"user_id":16537593},{"uid":49798167,"first_name":"Алёна","last_name":"Гайдаржи","photo_200":"https://pp.vk.me/c630021/v630021167/4c08b/4tEQgjdnKL8.jpg","online":0,"user_id":49798167},{"uid":252564215,"first_name":"Тёма","last_name":"Глухов","photo_200":"http://vk.com/images/camera_200.png","online":0,"user_id":252564215},{"uid":58178099,"first_name":"Владислав","last_name":"Багас","photo_200":"https://pp.vk.me/c630720/v630720099/5693c/zXAZYbRDuvI.jpg","online":0,"user_id":58178099},{"uid":346688131,"first_name":"Максим","last_name":"Романчу��","photo_200":"http://vk.com/images/camera_200.png","online":0,"user_id":346688131},{"uid":278760240,"first_name":"Вячеслав","last_name":"Третьяк","photo_200":"https://pp.vk.me/c638726/v638726240/961d/n6SWYdprzyQ.jpg","online":1,"user_id":278760240},{"uid":92281048,"first_name":"Данил","last_name":"Иванченко","photo_200":"https://pp.vk.me/c836227/v836227048/5d71/O7Cf1rCzD84.jpg","online":1,"user_id":92281048},{"uid":95470909,"first_name":"Дима","last_name":"Солоха","photo_200":"https://pp.vk.me/c637629/v637629909/8a91/xeQbIC7Y8pQ.jpg","online":1,"user_id":95470909},{"uid":307373374,"first_name":"Татьяна","last_name":"Мележик","photo_200":"https://pp.vk.me/c626531/v626531374/3af4d/EcgKeILs5gY.jpg","online":0,"user_id":307373374},{"uid":158643239,"first_name":"Арина","last_name":"Хаетинова","photo_200":"https://pp.vk.me/c622020/v622020239/57cef/CGzq-4d-Crs.jpg","online":1,"user_id":158643239},{"uid":95262410,"first_name":"Виталий","last_name":"Козюбердин","photo_200":"https://pp.vk.me/c636517/v636517410/2d3c5/dzcZu-zu9oI.jpg","online":1,"user_id":95262410},{"uid":97059808,"first_name":"Максим","last_name":"Коваль","photo_200":"https://pp.vk.me/c626129/v626129808/40505/EwV8zssFM0w.jpg","online":1,"user_id":97059808},{"uid":360469744,"first_name":"Sweet","last_name":"Dream","photo_200":"https://pp.vk.me/c636325/v636325744/21684/_kd-1MEYC8o.jpg","online":0,"user_id":360469744},{"uid":153451537,"first_name":"Даниил","last_name":"Тупикин","photo_200":"https://pp.vk.me/c636920/v636920537/34414/vbPrYQSCkEA.jpg","online":0,"user_id":153451537},{"uid":140258320,"first_name":"Артём","last_name":"Науменко","photo_200":"https://pp.vk.me/c626231/v626231320/29aee/aUW0GqRTldQ.jpg","online":0,"user_id":140258320},{"uid":224279414,"first_name":"Nikolai","last_name":"Oleinikov","photo_200":"https://pp.vk.me/c631126/v631126414/13357/txxD5w111xE.jpg","online":0,"user_id":224279414},{"uid":125443236,"first_name":"Вероника","last_name":"Макарова","photo_200":"https://pp.vk.me/c631217/v631217236/4d159/8U0EFq44Hc8.jpg","online":1,"user_id":125443236},{"uid":312199888,"first_name":"Ruslan","last_name":"Fomin","photo_200":"https://pp.vk.me/c837123/v837123888/fc32/O6BhqFOkLhM.jpg","online":0,"user_id":312199888},{"uid":269866199,"first_name":"Aleksei","last_name":"Shatun","photo_200":"https://pp.vk.me/c625625/v625625199/3a45b/4MPP3WJ48PM.jpg","online":1,"user_id":269866199},{"uid":226245488,"first_name":"Oksana","last_name":"Luchsheva","photo_200":"https://pp.vk.me/c626816/v626816488/1334d/aTkeE9MxHXQ.jpg","online":0,"user_id":226245488},{"uid":137576524,"first_name":"Денис","last_name":"Белов","photo_200":"https://pp.vk.me/c633619/v633619524/3aff9/6ekQquuB8vY.jpg","online":1,"user_id":137576524},{"uid":56903764,"first_name":"Дарья","last_name":"Костик","photo_200":"https://pp.vk.me/c636724/v636724764/26a22/qkkG4mNzWn4.jpg","online":1,"user_id":56903764},{"uid":148036443,"first_name":"Ксюша","last_name":"Зиннурова","photo_200":"https://pp.vk.me/c631824/v631824443/1f488/akrfnln3jMQ.jpg","online":1,"user_id":148036443},{"uid":170173889,"first_name":"Руслан","last_name":"Курганов","photo_200":"https://pp.vk.me/c637430/v637430889/db22/fikAPt60928.jpg","online":0,"user_id":170173889},{"uid":53792287,"first_name":"Влад","last_name":"Подоляка","photo_200":"https://pp.vk.me/c630326/v630326287/2a1dd/_JyVECnwiYk.jpg","online":1,"user_id":53792287},{"uid":81095988,"first_name":"Вадим","last_name":"Гурьев","photo_200":"https://pp.vk.me/c623619/v623619988/1981c/OMsqLeLyHt8.jpg","online":1,"user_id":81095988},{"uid":71964548,"first_name":"Александр","last_name":"Омельченко","photo_200":"https://pp.vk.me/c627128/v627128548/148b2/xu0wPEnwO0A.jpg","online":0,"user_id":71964548},{"uid":147014443,"first_name":"Лёха","last_name":"Бредиха","photo_200":"https://pp.vk.me/c636029/v636029443/38980/-mIORkPjJeE.jpg","online":1,"user_id":147014443},{"uid":185904317,"first_name":"Анастасия","last_name":"Злобина","photo_200":"https://pp.vk.me/c630030/v630030317/61872/OCnerXEzD7Y.jpg","online":1,"user_id":185904317},{"uid":279613609,"first_name":"Alena","last_name":"Sergeevna","photo_200":"https://pp.vk.me/c633324/v633324609/247e7/38hzgYyXXWw.jpg","online":1,"user_id":279613609}]}
LOG: it is 54 iteration, but name is Vladislava
LOG: it is 54 iteration, but name is Артём
LOG: it is 54 iteration, but name is Алёна
LOG: it is 54 iteration, but name is Денис
LOG: it is 54 iteration, but name is Влад
LOG: it is 54 iteration, but name is Назар
LOG: it is 54 iteration, but name is Даша
LOG: it is 54 iteration, but name is Андрей
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Роман
LOG: it is 54 iteration, but name is Влад
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Миша
LOG: it is 54 iteration, but name is Максим
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Андрей
LOG: it is 54 iteration, but name is Владимир
LOG: it is 54 iteration, but name is Дарья
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Alexandra
LOG: it is 54 iteration, but name is Вова
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Евгений
LOG: it is 54 iteration, but name is Алексей
LOG: it is 54 iteration, but name is Денис
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is User
LOG: it is 54 iteration, but name is Анна
LOG: it is 54 iteration, but name is Аня
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Руслан
LOG: it is 54 iteration, but name is Алёна
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Владислав
LOG: it is 54 iteration, but name is Тёма
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Кирилл
LOG: it is 54 iteration, but name is Максим
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Данил
LOG: it is 54 iteration, but name is Вячеслав
LOG: it is 54 iteration, but name is Дима
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Арина
LOG: it is 54 iteration, but name is Виталий
LOG: it is 54 iteration, but name is Максим
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Sweet
LOG: it is 54 iteration, but name is Татьяна
LOG: it is 54 iteration, but name is Nikolai
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Даниил
LOG: it is 54 iteration, but name is Вероника
LOG: it is 54 iteration, but name is Артём
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Денис
LOG: it is 54 iteration, but name is Ruslan
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Oksana
LOG: it is 54 iteration, but name is Дарья
LOG: it is 54 iteration, but name is Aleksei
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Ксюша
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Влад
LOG: it is 54 iteration, but name is Анастасия
LOG: it is 54 iteration, but name is Александр
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Вадим
LOG: it is 54 iteration, but name is Руслан
LOG: it is 54 iteration, but name is Лёха
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: errTypeError: Cannot read property '0' of undefined
LOG: it is 54 iteration, but name is Alena
LOG: errTypeError: Cannot read property '0' of undefined

Maybe stepping through the code in a debugger would make the error easier to find? We have a tutorial video for that: https://www.youtube.com/watch?v=GloEpBr2lK0