A minimal promise polyfill.
If you cannot rely on native promises and you can't use a huge promise polyfill then minp
has got you covered!
Minp
is a barebones promise polyfill it does not nearly cover the whole promise specification but it is really tiny(<30 loc). Minp
is meant to be used in libraries that want to offer promise goodness to everyone without having to ship huge polyfills.
$ npm install --save minp
<script src="https://unpkg.com/minp"></script>
const P = require('minp');
const promiseFunction = (args) => new P ((resolve, reject) => {
if (args === 'awesome') {
resolve('yes', 'it', 'is');
} else {
reject(new Error('Nope!'));
}
});
promiseFunction('awesome').then((arg1, arg2, arg3) => {
console.log(arg1, arg2, arg3);
// => 'yes it is'
}).catch((err) => {
console.error(err);
});
Type: function
Whatever you want - it's just a function.
Type: function
You should call resolve
if everything worked. You can pass whichever arguments you want, they will be handed over to .then
. If you resolve the promise .then
will be executed.
Type: function
You should call reject
if an error occurred. Just pass the error as an argument. If you reject the promise .catch
will be executed.
MIT © Tobias Herber