A few recommended practices for writing good JavaScript asynchronous code

Today, I will recommend a few recommended practices for writing JavaScript asynchronous code. Each scenario has a corresponding eslint rule, and you can choose to configure it.

no-async-promise-executor

Passing an async function to the constructor of new Promise is not recommended.

 // ❌ new Promise(async (resolve, reject) => {}); // ✅ new Promise((resolve, reject) => {});

First, if you use async in the Promise ‘s constructor, wrapping a Promise may not be necessary. In addition, if the async function throws an exception, the newly constructed promise instance will not reject , so the error cannot be caught. …

The post A few recommended practices for writing good JavaScript asynchronous code first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9946
This site is for inclusion only, and the copyright belongs to the original author.