Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 103
  • Last Modified:

Please explain a jQuery promise

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
newbieweb
Asked:
newbieweb
  • 3
  • 2
2 Solutions
 
Julian HansenCommented:
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
 
newbiewebSr. Software EngineerAuthor Commented:
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
 
Julian HansenCommented:
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
 
newbiewebSr. Software EngineerAuthor Commented:
thanks
0
 
Julian HansenCommented:
You are welcome.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now