Let's say I have a synchronous function like path.join()
. I want to wrap it into a Promise
because I want exceptions to be handled within catch()
block.
If I wrap it like below, I do not get an exception in the Promise
's .catch()
block. So I have to use if
to check the return value for whether it's an error or not and then call resolve
or reject
functions. Are there any other solutions?
var joinPaths = function(path1,path2) {
return new promise(function (resolve, reject) {
resolve(path.join(path1, path2));
});
};
Very quick way is to wrap prepend a function with async
keyword. Any async function returns a Promise
const sum = async (a, b) => a + b;
sum(1, 2).then(value => console.log(value))
Not sure if it's the best way but it works. I came to StackOverflow to check if there's a better way.
new Promise((resolve, reject) => {
try {
resolve(path.join());
} catch (err) {
reject(err);
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With