Solved

Please explain a jQuery promise

Posted on 2016-10-18
5
48 Views
Last Modified: 2016-10-18
This comes from here:
https://www.promisejs.org/patterns/

I am not sure what key words are defined by jQuery versus what is needed in this implementation...

var maybePromise = Math.random() > 0.5 ? 10 : Promise.resolve(10);
var definitelyPromise = Promise.resolve(maybePromise);
// equivalent to
var definitelyPromise = new Promise(function (fulfill, reject) {
  if (isPromise(maybePromise)) {
    maybePromise.then(fulfill, reject);
  } else {
    fulfill(maybePromise);
  }
});
0
Comment
Question by:newbieweb
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 41848626
None of the code is jQuery specific - it is all plain JavaScript

The example was about how to convert something that may or may not be a promise into a genuine promise so you can perform all the promisy things on it.
0
 

Author Comment

by:newbieweb
ID: 41848667
So, Promise in an object defined in the javascript library?

Anything that starts with Promise. is a method also defined in the  javascript library?

What about isPromise() ?

fulfill()
0
 
LVL 53

Assisted Solution

by:Julian Hansen
Julian Hansen earned 500 total points
ID: 41848706
That document describes Promises but the code is not strictly native JavaScript. The functions isPromise and fulfill are not methods on the promise - I imagine they are implied functions that would perform the operations of determining if an object is a Promise (has a .then() method for example) and fulfill (resolving the promise).

You can read more about the JavaScript promise here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
0
 

Author Closing Comment

by:newbieweb
ID: 41848741
thanks
0
 
LVL 53

Expert Comment

by:Julian Hansen
ID: 41848756
You are welcome.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now