How to convert JavaScript callbacks to Promises? Method introduction
A few years ago, callbacks were the only way to execute asynchronous code in JavaScript. There are few problems with callbacks themselves, the most notable being "callback hell".
Introduced in ES6 Promise as a solution to these problems. Finally, the async/await
keyword is introduced to provide a better experience and improve readability.
Even with the new methods, there are still many native modules and libraries that use callbacks. In this article, we will discuss how to convert JavaScript callbacks into Promises. Knowledge of ES6 will come in handy, as we'll be using features like the spread operator to simplify what we have to do.
What is a callback
A callback is a function parameter, which happens to be a function itself. Although we can create any function to accept another function, callbacks are mainly used for asynchronous operations.
JavaScript is an interpreted language that can only process one line of code at a time. Some tasks may take a long time to complete, such as downloading or reading large files, etc. JavaScript offloads these long-running tasks to the browser or other processes within the Node.js environment. This way it won't block other code from executing.
Usually asynchronous functions will accept callback functions, so their data can be processed after completion.
For example, we will write a callback function that will be executed after the program successfully reads the file from the hard disk.
So you need to prepare a text file named sample.txt
, which contains the following content:
Hello world from sample.txt
Then write a simple Node.js script to read the file :
const fs = require('fs'); fs.readFile('./sample.txt', 'utf-8', (err, data) => { if (err) { // 处理错误 console.error(err); return; } console.log(data); }); for (let i = 0; i < 10; i++) { console.log(i); }
After running the code, it will output:
0 ... 8 9 Hello world from sample.txt
If you use this code, you should see 0..9
being output to the console before executing the callback. This is because of JavaScript's asynchronous management mechanism. After reading the file, the callback for outputting the file content is called.
By the way, callbacks can also be used in synchronous methods. For example Array.sort()
will accept a callback function that allows you to customize the sorting of elements.
Functions that accept callbacks are called "higher-order functions".
Now we have a better callback method. So let's continue to look at what Promise is.
What is Promise
Promise was introduced in ECMAScript 2015 (ES6) to improve the experience of asynchronous programming. As the name suggests, the "value" or "error" that a JavaScript object will eventually return should be a Promise.
A Promise has 3 states:
- Pending (pending) : The initial state used to indicate that the asynchronous operation has not yet been completed.
- Fulfilled: Indicates that the asynchronous operation has been completed successfully.
- Rejected: Indicates that the asynchronous operation failed.
Most Promises end up looking like this:
someAsynchronousFunction() .then(data => { // promise 被完成 console.log(data); }) .catch(err => { // promise 被拒绝 console.error(err); });
Promises are very important in modern JavaScript because they are the same as introduced in ECMAScript 2016 Use async/await keywords together. Using
async / await eliminates the need to write asynchronous code with callbacks or
then() and
catch().
try { const data = await someAsynchronousFunction(); } catch(err) { // promise 被拒绝 console.error(err); }
async/await keyword.
fs module) There is a standard way to do this: pass the callback as the last parameter.
fs.readFile() without specifying the text encoding:
fs.readFile('./sample.txt', (err, data) => { if (err) { console.error(err); return; } console.log(data); });
Note: If you specify utf-8 as the encoding, the resulting output is a string. If not specified the output is
Buffer.
Error since it is the first parameter. There can be any number of outputs afterwards.
const util = require('util');
promisify method to convert it to a Promise:
const fs = require('fs'); const readFile = util.promisify(fs.readFile);
readFile('./sample.txt', 'utf-8') .then(data => { console.log(data); }) .catch(err => { console.log(err); });
async/await keyword given in the example below:
const fs = require('fs'); const util = require('util'); const readFile = util.promisify(fs.readFile); (async () => { try { const content = await readFile('./sample.txt', 'utf-8'); console.log(content); } catch (err) { console.error(err); } })();
async Use the
await keyword in the created function, which is why a function wrapper is used. Function wrappers are also known as immediately invoked function expressions.
如果你的回调不遵循这个特定标准也不用担心。 util.promisify()
函数可让你自定义转换是如何发生的。
注意: Promise 在被引入后不久就开始流行了。 Node.js 已经将大部分核心函数从回调转换成了基于 Promise 的API。
如果需要用 Promise 处理文件,可以用 Node.js 附带的库(https://nodejs.org/docs/lates...)。
现在你已经了解了如何将 Node.js 标准样式回调隐含到 Promise 中。从 Node.js 8 开始,这个模块仅在 Node.js 上可用。如果你用的是浏览器或早期版本版本的 Node,则最好创建自己的基于 Promise 的函数版本。
创建你自己的 Promise
让我们讨论一下怎样把回调转为 util.promisify()
函数的 promise。
思路是创建一个新的包含回调函数的 Promise 对象。如果回调函数返回错误,就拒绝带有该错误的Promise。如果回调函数返回非错误输出,就解决并输出 Promise。
先把回调转换为一个接受固定参数的函数的 promise 开始:
const fs = require('fs'); const readFile = (fileName, encoding) => { return new Promise((resolve, reject) => { fs.readFile(fileName, encoding, (err, data) => { if (err) { return reject(err); } resolve(data); }); }); } readFile('./sample.txt') .then(data => { console.log(data); }) .catch(err => { console.log(err); });
新函数 readFile()
接受了用来读取 fs.readFile()
文件的两个参数。然后创建一个新的 Promise 对象,该对象包装了该函数,并接受回调,在本例中为 fs.readFile()
。
要 reject
Promise 而不是返回错误。所以代码中没有立即把数据输出,而是先 resolve
了Promise。然后像以前一样使用基于 Promise 的 readFile()
函数。
接下来看看接受动态数量参数的函数:
const getMaxCustom = (callback, ...args) => { let max = -Infinity; for (let i of args) { if (i > max) { max = i; } } callback(max); } getMaxCustom((max) => { console.log('Max is ' + max) }, 10, 2, 23, 1, 111, 20);
第一个参数是 callback 参数,这使它在接受回调的函数中有点与众不同。
转换为 promise 的方式和上一个例子一样。创建一个新的 Promise 对象,这个对象包装使用回调的函数。如果遇到错误,就 reject
,当结果出现时将会 resolve
。
我们的 promise 版本如下:
const getMaxPromise = (...args) => { return new Promise((resolve) => { getMaxCustom((max) => { resolve(max); }, ...args); }); } getMaxCustom(10, 2, 23, 1, 111, 20) .then(max => console.log(max));
在创建 promise 时,不管函数是以非标准方式还是带有许多参数使用回调都无关紧要。我们可以完全控制它的完成方式,并且原理是一样的。
总结
尽管现在回调已成为 JavaScript 中利用异步代码的默认方法,但 Promise 是一种更现代的方法,它更容易使用。如果遇到了使用回调的代码库,那么现在就可以把它转换为 Promise。
在本文中,我们首先学到了如何 在Node.js 中使用 utils.promisfy()
方法将接受回调的函数转换为 Promise。然后,了解了如何创建自己的 Promise 对象,并在对象中包装了无需使用外部库即可接受回调的函数。这样许多旧 JavaScript 代码可以轻松地与现代的代码库和混合在一起。
更多编程相关知识,请访问:编程学习!!
The above is the detailed content of How to convert JavaScript callbacks to Promises? Method introduction. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

In daily life, we often encounter problems between promises and fulfillment. Whether in a personal relationship or a business transaction, delivering on promises is key to building trust. However, the pros and cons of commitment are often controversial. This article will explore the pros and cons of commitments and give some advice on how to keep your word. The promised benefits are obvious. First, commitment builds trust. When a person keeps his word, he makes others believe that he is a trustworthy person. Trust is the bond established between people, which can make people more

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a
