Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Please explain a jQuery promise

Posted on 2016-10-18
5
Medium Priority
?
96 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 60

Accepted Solution

by:
Julian Hansen earned 2000 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 60

Assisted Solution

by:Julian Hansen
Julian Hansen earned 2000 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 60

Expert Comment

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

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
The viewer will learn how to dynamically set the form action using jQuery.
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)
Suggested Courses

885 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