


Quickly avoid pitfalls and talk about 5 common mistakes in using promises!
This article will share with you 5 common mistakes when using promises to help you avoid pitfalls quickly. I hope it will be helpful to you!
#Promise provides an elegant way to handle asynchronous operations in JS. This is also a solution to avoid "callback hell". However, not many developers understand what's involved. Therefore, many people tend to make mistakes in practice. [Related recommendations: javascript learning tutorial]
In this article, we introduce five common mistakes when using promises, and hope that everyone can avoid these mistakes.
1. Avoid Promise Hell
Usually, Promise is used to avoid callback hell. But abusing them can also lead to Promises being hell.
userLogin('user').then(function(user){ getArticle(user).then(function(articles){ showArticle(articles).then(function(){ //Your code goes here... }); }); });
In the above example, we have three nested promises for userLogin
, getararticle
and showararticle
. This way the complexity will grow in proportion to the lines of code and it may become unreadable.
To avoid this, we need to unnest the code, return getArticle
from the first then
, and then in the second then
to handle it.
userLogin('user') .then(getArticle) .then(showArticle) .then(function(){ //Your code goes here... });
2. Use <span style="font-size: 18px;">try/catch</span>
block
Normally, we use try/catch
blocks to handle errors. However, using try/catch
within a Promise
object is not recommended.
This is because if there are any errors, the Promise object will be automatically handled within catch
.
ew Promise((resolve, reject) => { try { const data = doThis(); // do something resolve(); } catch (e) { reject(e); } }) .then(data => console.log(data)) .catch(error => console.log(error));
In the above example, we used the try/catch
block inside the Promise.
However, Promise itself will catch all errors (even typos) within its scope without the need for a try/catch
block. It ensures that all exceptions thrown during execution are caught and converted into rejected Promises.
new Promise((resolve, reject) => { const data = doThis(); // do something resolve() }) .then(data => console.log(data)) .catch(error => console.log(error));
Note: It is crucial to use the .catch()
block within a Promise block. Otherwise, your test cases may fail and the application may crash during the production phase.
3. Use asynchronous functions within Promise blocks
Async/Await
is a more advanced syntax, Used to handle multiple Promises in synchronous code. When we use the async
keyword before a function declaration, it will return a Promise. We can use the await
keyword to stop the code until the Promise we are waiting for is resolved or rejected. .
However, when you put an Async function inside a Promise block, there will be some side effects.
Suppose we want to do an asynchronous operation in the Promise block, so we use the async
keyword, but unfortunately our code throws an error.
This way, even if you use a catch()
block or wait for your Promise inside a try/catch
block, we cannot handle the error immediately. Please see the example below.
// 此代码无法处理错误 new Promise(async () => { throw new Error('message'); }).catch(e => console.log(e.message)); (async () => { try { await new Promise(async () => { throw new Error('message'); }); } catch (e) { console.log(e.message); } })();
When I encounter an async
function inside a Promise block, I'm trying to keep the async
logic outside the Promise block to keep it synchronized. It works 9 times out of 10.
However, in some cases, an async
function may be required. In this case, there is no choice but to manage it manually using try/catch
blocks.
new Promise(async (resolve, reject) => { try { throw new Error('message'); } catch (error) { reject(error); } }).catch(e => console.log(e.message)); //using async/await (async () => { try { await new Promise(async (resolve, reject) => { try { throw new Error('message'); } catch (error) { reject(error); } }); } catch (e) { console.log(e.message); } })();
4. Execute the Promise block immediately after creating the Promise
As for the following code snippet, if we put the code snippet in the call HTTP Where requested, it will be executed immediately.
const myPromise = new Promise(resolve => { // code to make HTTP request resolve(result); });
The reason is that this code is wrapped in a Promise constructor. However, some may think that it is only triggered after the then
method of myPromise
is executed.
However, the truth is not so. In contrast, when a Promise is created, the callback is executed immediately.
This means that by the time the following line is reached after establishing myPromise
, the HTTP request is most likely already running, or at least in a scheduled state.
Promises are always eager to execute the process.
But what should you do if you want to execute Promises in the future? What if I don't want to make an HTTP request now? Is there some magical mechanism built into Promises that allows us to do this?
The answer is to use functions. Functions are a time-consuming mechanism. They will only execute if the developer explicitly calls them with ()
. Simply defining a function doesn't get us anywhere. So, the most efficient way to make a Promise lazy is to wrap it in a function!
const createMyPromise = () => new Promise(resolve => { // HTTP request resolve(result); });
对于HTTP请求,Promise 构造函数和回调函数只有在函数被执行时才会被调用。所以现在我们有一个懒惰的Promise,只有在我们需要的时候才会执行。
5. 不一定使用 Promise.all() 方法
如果你已经工作多年,应该已经知道我在说什么了。如果有许多彼此不相关的 Promise,我们可以同时处理它们。
Promise 是并发的,但如你一个一个地等待它们,会太费时间,Promise.all()
可以节省很多时间。
记住,Promise.all() 是我们的朋友
const { promisify } = require('util'); const sleep = promisify(setTimeout); async function f1() { await sleep(1000); } async function f2() { await sleep(2000); } async function f3() { await sleep(3000); } (async () => { console.time('sequential'); await f1(); await f2(); await f3(); console.timeEnd('sequential'); })();
上述代码的执行时间约为 6
秒。但如果我们用 Promise.all()
代替它,将减少执行时间。
(async () => { console.time('concurrent'); await Promise.all([f1(), f2(), f3()]); console.timeEnd('concurrent'); })();
总结
在这篇文章中,我们讨论了使用 Promise 时常犯的五个错误。然而,可能还有很多简单的问题需要仔细解决。
如果你还有更多相关的错误,欢迎留言一起讨论。
英文原文地址:https://blog.bitsrc.io/5-common-mistakes-in-using-promises-bfcc4d62657f
作者:Ravidu Perera
更多编程相关知识,请访问:编程入门!!
The above is the detailed content of Quickly avoid pitfalls and talk about 5 common mistakes in using promises!. For more information, please follow other related articles on the PHP Chinese website!

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base

JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.


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

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.