12345678910111213141516171819202122 |
- // cannot use fat arrow function since we don't get the arguments, then
- const promisify = (fn, context = null) => function () {
- // copy passed arguments to a new array
- const args = toArray(arguments);
- return new Promise((resolve, reject) => {
- // add a callback to the arguments, which resolves the promise with the result
- args.push((err, data) => {
- if (err) {
- reject(err);
- } else {
- resolve(data);
- }
- });
- // call the original function with our callback flavoured arguments
- fn.apply(context, args);
- });
- };
- export default promisify;
|