Link to home
Start Free TrialLog in
Avatar of Marco Gasi
Marco GasiFlag for Spain

asked on

jQuery promises in sequence

Hi all.
This question follows this one: https://www.experts-exchange.com/questions/28923201/javascript-function-return-problem.html
I use jQuery promises to get some data and dinamically build an object to pass handlebars.compile. Here's the code:
function getFolders2() {
	var foldersContext = {};
	getFolderCardsNumber('PC').success(function (result) {
		var pcContext = {pc_cards_number: result};
		jQuery.extend(foldersContext, pcContext);
	});
	getFolderCardsNumber('SP').success(function (result) {
		var spContext = {sp_cards_number: result};
		jQuery.extend(foldersContext, spContext);
	});
	console.log('first context:');
	console.log(foldersContext);
	getCustomFolders.success(function (data) {
		var response = JSON.parse(data);
		if (response.success) {
			var folders = response.result;
			if (folders.length > 0) {
				var i = 0, l = folders.length;
				var id, description;
				for (i;i < l;i++) {
					id = folders[i].folder_id;
					description = folders[i].folder_description;
					getFolderCardsNumber(id).success(function (result) {
						var customFolders = {
							fd_cards_number: result,
							folder_id: id,
							folder_description: description
						};
						jQuery.extend(foldersContext, customFolders);
					});
				}
				console.log('second context:');
				console.log(foldersContext);
				var html = parseAndReturnHandlebarsRev2("folders2", foldersContext);
				console.log(html);
				jQuery('.all-folders').html(html);
			}
		}
	});
};

function getCustomFolders() {
	return jQuery.ajax({
		type: "GET",
		url: "includes/manage-folders/get_folders.json.php",
		data: {}
	});
};

function getFolderCardsNumber(folder) {
	return jQuery.ajax({
		type: 'post',
		url: 'includes/card-contacts/get_folder_cards_number.php',
		data: {folder: folder}
	});
};

Open in new window

Well, the first two calls to external function getFolderCardsNumber() work fine and the object foldersContext is correctly built, but then I receive this error:

TypeError: getCustomFolders.success is not a function
    getCustomFolders.success(function (data) {

Open in new window


Do I need a couple of fresh eyes?
Thank you.
ASKER CERTIFIED SOLUTION
Avatar of Alexandre Simões
Alexandre Simões
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi

ASKER

Lol, I said I needed a couple of fresh eyes! Thank you, Alex. Probably a new question soon :-)
Thank you
Hi Alex. Here the link to my last question: the last step in this task :-) https://www.experts-exchange.com/questions/28923510/push-strange-behavior.html 
Thank you