AnyPromise is an implementation-agnostic JavaScript interface for Promises, based on the ES6 Promise specification.
Libraries requiring Promises can depend on the interface while letting the caller provide the implementation. That way, a library doesn't have to explicitly depend on a given Promise library; it merely depends on the abstract interface.
/* == library.js == */
export function timeoutPromise(Promise, delay) {
return Promise((resolve, reject) => {
setTimeout(resolve, delay);
});
};
/* == consumer.js == */
import {timeoutPromise} from 'library';
import {Promise} from 'any-promise-es6';
// or: import {Promise} from 'any-promise-rsvp';
timeoutPromise(Promise, 1000).then(() => {
console.log("done waiting 1 second!");
});
timeoutPromise
provides an abstract functionality that depends on a
Promise interface, but not a specific implementation; the
implementation is provided by the caller.
TODO: describe the interface
Adapters are thin wrapper libraries that implement the AnyPromise interface on top of existing Promise libraries: