mono/packages/fs/dist/promisify.js
2025-01-23 07:22:43 +01:00

26 lines
850 B
JavaScript

export function promisify(f, thisContext) {
return function () {
const args = Array.prototype.slice.call(arguments);
return new Promise((resolve, reject) => {
args.push((err, result) => err !== null ? reject(err) : resolve(result));
f.apply(thisContext, args);
});
};
}
export function map(elts, f) {
const apply = (appElts) => Promise.all(appElts.map((elt) => typeof elt.then === 'function' ? elt.then(f) : f(elt)));
return typeof elts.then === 'function' ? elts.then(apply) : apply(elts);
}
export function _try(f, thisContext) {
const args = Array.prototype.slice.call(arguments);
return new Promise((res, rej) => {
try {
args.shift();
res(f.apply(thisContext, args));
}
catch (err) {
rej(err);
}
});
}