jquery deferred question

I've just seen this syntax:
return $.Deferred().resolve().promise();
?
Is the $.Deferred a singleton? Can there be multiple different ones, and what's the point in resolving it without attaching any functions to it, there's nothing to resolve is there?
Silas2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobOwner (Aidellio)Commented:
Yes it is for all intents and purposes a singleton.  It is used as a kind of stack for events and callbacks and you are deferring them until you're ready to process, which happens when .resolve() is called.  Adding the .promise() will mean that the Deferred object will not allow any further additions until the stack is empty (i.e. .resolve() has finished)

When this is returned, it is returning an object that will allow callbacks to be added but they will not be processed as part of the previous .resolve process.
0
Silas2Author Commented:
So is it saying that the singleton $.Deferred() object/call, if it had any procedure attached somewhere else in the code, by calling the .resolve(), would execute all of those prior to returning the promise?

And those procedures, would they all have to have returned , or are they just called?
0
RobOwner (Aidellio)Commented:
The return to insignificant. What you're saying is correct except that more callbacks can be attached while the deferred is still executing bit they will not be processed until the deferred had finished the previous set
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Silas2Author Commented:
Thanks for your help, and sorry to go on, I find this hard to get my head round....
So the upshot of this line is to clear pending calls?
0
RobOwner (Aidellio)Commented:
Yes and returns the promise object that lets you to know when all callbacks have competed

https://api.jquery.com/promise/

Don't worry, this stuff does my head in too!  In what context are you planning on using it?
0
Silas2Author Commented:
I'm working on a large project and the architect has used this pattern, so I feel a bit embarrassed not understanding fully.
0
RobOwner (Aidellio)Commented:
Sure, I can understand that.  Can you see what they've attached to the deferred object?
0
Silas2Author Commented:
This is in typescript , but obviously javascript underneath
export class EmployeeView extends Observable {
		public viewName: string;
		public element: JQuery;
		
		
		public canClose(): JQueryPromise<any> {
			return $.Deferred().resolve().promise();
		}

Open in new window

Then an Administration page, which creates a view:
        export class EmployeePage {
		public tab: Tab;
		public view: EmployeeView;
		public viewElement: JQuery;

		constructor() {
			this._init();
		}
              private _onItemClick(e: temClickEvent): JQueryPromise<any> {
			var view = this.view,
				promise: JQueryPromise<any>;

			promise = view.canClose();

			promise = promise.then(() => this._showView(e.item.name));

			return promise;
		}

Open in new window

Then the showView procedure, makes an ajax call with another def
        private _showView(name: string): JQueryPromise<any> {
			var viewElement = this.viewElement,
				view = this.view,
				ajaxOptions: AjaxOptions,
				promise: JQueryPromise<any>;

			ajaxOptions = {
				url: "Employee/" + name + "/Index",
			}

			curtains.show();

			promise = $.ajax(ajaxOptions);

			promise.done((viewString: string) => {
				if (view) {
					view.destroy();
					view = null;
				}

				viewElement.html(viewString);
				view = this.view = this._createView(name);
			});

			promise.always(() => {
				curtains.hide();
			});

			return promise;
		}

Open in new window

0
RobOwner (Aidellio)Commented:
Sure ok... so it's not going to show the view (showView) until all the Deferred objects have completed.

What part or link between the code you've posted do you not understand and I'll walk you through it.

I'll give you an overview now:

EmpoyeeView class is created with the one public method "canClose()".  When this is called you know that any Deferred objects will be processed with the .resolve() function and chaining the .promise() will prevent other code from changing the Deferred's state.  That's why it's called "canClose" because it's checking that it is allowed to close the window.  Just a fancy way of making sure that things happen in the order you expect.
.then() is used to add a callback when the Deferred object has been resolved and when it has then show the view.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Silas2Author Commented:
Thanks very much for your help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.