📅  最后修改于: 2023-12-03 15:03:11.961000             🧑  作者: Mango
In Node.js, many asynchronous functions are callback-based where the last parameter of the function is a callback function that is called once the operation completes. While callback-based functions work well, working with them in a more complex application can be quite cumbersome. A better approach is to use Promises.
Promises allow us to write more concise and expressive code to handle asynchronous operations in a more functional manner. In this article, we will discuss how to use Promisify without err in Javascript.
Promisify is a Node.js utility function that converts callback-based functions into Promise-based functions. It takes a function that conforms to the Node.js callback style and returns a new version of the function that returns a Promise instead.
To see how Promisfy works consider the following example. It takes a callback-style function that accepts two arguments and logs the sum of these arguments:
function add(a, b, cb) {
cb(null, a + b);
}
add(1, 2, (err, result) => {
if (err) {
console.error(err);
} else {
console.log(result);
}
});
This code works as expected, but it is not ideal for error handling or chaining multiple async operations. Here's how we can use Promisify to improve the code:
const { promisify } = require('util');
const add = (a, b, callback) => {
callback(null, a + b);
};
const addAsync = promisify(add);
addAsync(1, 2)
.then((result) => console.log(result))
.catch((error) => console.error(error));
In this example, we create a new function named addAsync
by using the promisify
method from the built-in util
module. This function takes the original add
function as input and returns a new function that returns a promise.
Now, we can call addAsync
and handle the resulting promise's success and error. In the promise chain, we can make multiple promise-style function calls with ease.
As we know, Promisify includes error-handling as part of its implementation. However, in some cases, error-handling must be done manually. Promisify without err is a utility function that can be used to convert a callback-style function into a Promise-style function without error handling, thus handling errors manually as required.
const promisifyWithoutErr = (fn) => {
return (...args) =>
new Promise((resolve) => {
fn(...args, (result) => {
resolve(result);
});
});
};
const wait = (ms, cb) => {
setTimeout(() => {
cb(Math.floor(Math.random() * 100));
}, ms);
};
const waitAsync = promisifyWithoutErr(wait);
waitAsync(1000)
.then((result) => console.log(result))
.catch((error) => console.error(error)); // No automatic error handling
Here, promisifyWithoutErr
is a custom implementation of Promisify without err. We have defined it using an arrow function that takes a function fn
as input and returns a new function that returns a promise. The new Promise
call here takes a resolve function as input, which is called within the callback function passed to the original function fn
. Thus, this implementation does not handle any errors within Promisify.
We also have a function wait
which is a simple implementation of an asynchronous function with a callback. We can pass it as input to promisifyWithoutErr
to obtain the promise-based function waitAsync
.
We can then use waitAsync
in a similar fashion to any other promise-based function, chaining it with other promise-based functions as necessary.
Promisify is a helpful utility function in Node.js that converts callback-style functions into promise-based ones. We can also use Promisify without err to convert functions into Promises without internal error handling. This allows us to handle errors in the calling code in a way that best suits our requirements. This approach to promise-based programming enables us to write concise and functional code that is readable and easy to understand.