Solved

Please explain a jQuery promise

Posted on 2016-10-18
5
35 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 51

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 51

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 51

Expert Comment

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

There are a couple ways to attach a JavaScript function to dynamically created elements. You can make a new script for each element as it’s created or you can use delegation. Delegation allows a single script that is added at page creation to mat…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

747 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

15 Experts available now in Live!

Get 1:1 Help Now