Custom promise


A Promise is in one of these states:

  • pending: initial state, neither fulfilled nor rejected.
  • fulfilled: meaning that the operation was completed successfully.
  • rejected: meaning that the operation failed.

let timeoutPromse = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Success!');
  }, 2000);
});

timeoutPromise
.then((message) => {
   alert(message);
})

or even just

timeoutPromise.then(alert);

function timeoutPromise(message, interval) {
  return new Promise((resolve, reject) => {
    if (message === '' || typeof message !== 'string') {
      reject('Message is empty or not a string!');
   } else if (interval < 0 || typeof interval !== 'number'){
      reject('Interval is negative or not a number!');
  }else{
    setTimeout(()=> {resolve(message);}, interval)
}
  })
}

timeoutPromise('Hello', 1000)
.then(message => {
  alert(message);
})
.catch(e => {
  console.log('Error: ' + e);
});

The following block converts the basic request model used by many IndexedDB methods to use promises (see this code, for example).

function promisifyRequest(request) {
 |     const promise = new Promise((resolve, reject) => {
 |         const unlisten = () => {
 |             request.removeEventListener('success', success);
 |             request.removeEventListener('error', error);
 |         };
 |         const success = () => {
 |             resolve(wrap(request.result));
 |             unlisten();
 |         };
 |         const error = () => {
 |             reject(request.error);
 |             unlisten();
 |         };
 |         request.addEventListener('success', success);
 |         request.addEventListener('error', error);
 |     });

 |     promise
 |         .then((value) => {
 |         // Since cursoring reuses the IDBRequest (*sigh*), we cache it for later retrieval
 |         // (see wrapFunction).
 |         if (value instanceof IDBCursor) {
 |             cursorRequestMap.set(value, request);
 |         }
 |         // Catching to avoid "Uncaught Promise exceptions"
 |     })
 |         .catch(() => { });
 |     // This mapping exists in reverseTransformCache but doesn't doesn't exist in transformCache. This
 |     // is because we create many promises from a single IDBRequest.
 |     reverseTransformCache.set(promise, request);
 |     return promise;
 | }


https://github.com/jakearchibald/idb/
About
IndexedDB, but with promises
www.npmjs.com/package/idb


IndexedDB with usability.


This is a tiny (~1.09k brotli'd) library that mostly mirrors the IndexedDB API, but with small improvements that make a big difference to usability.

Most modern Web APIs are promise-based, so you'll need to understand promises to get the most out of them. Among those APIs are WebRTC, Web Audio API, Media Capture and Streams, and many more. Promises will be more and more important as time goes on, so learning to use and understand them is an important step in learning modern JavaScript.

As the Promise.prototype.then() and Promise.prototype.catch() methods return promises, they can be chained.



阅读量: 593
发布于:
修改于: