“This is the 15th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Reading data asynchronously is familiar, but there is a method of reading data called callback hell. Judging by its name, it’s a real headache for developers. Looks like a Russian πŸͺ† matryoshka πŸͺ†.

Because of asynchronous operations, the following code can be executed directly after the request is made without waiting for the result to be returned. If you have an interface whose request parameters are retrieved from the data returned by another interface, you have to make sure that the request from that interface occurs after the data returned by the other interface. If mishandled it can lead to callback hell, which can be confusing to look at and costly to maintain.

How to return from hell to earth? There are three avenues for small partners to choose, the rough slide back 😁

【 ε…Έ εž‹ δΎ‹ ε₯ 1: Promise 】

  • Two parametersResolve (), reject()
  • Chained programming ensures that programs can be executed sequentially.then().then().catch()

【 Smooth road 2: async/await 】

  • Async functions have asynchronous operations and can execute the following code without waiting for results
    • To use neither language nor await at the same time is meaningless.
  • Await an asynchronous operation to have the feel of a synchronous operation, waiting for the result before continuing to execute the following code
    • Must be used in conjunction with async
    • You can retrieve the result returned by promise.resolve
    • The awaited Promise instance must have a return state, otherwise await becomes ❄️ cool ❄️, and the code behind await will not be executed, waiting for nothing to blossom

【 Smooth road 3: Thunk 】

  • A method that can automatically execute Generator functions
  • Only the callback function is accepted as the only argument
  • Lazy evaluation function
  • Kerrization of multiple parameters
  • As long as the argument contains a callback function, it can be written as thunk

The solution to callback hell is to make the whole application look clean and clean, and to be able to operate asynchronously as well as synchronously. With Russia πŸͺ† dolls πŸͺ† say: “Byebye!!!!!!”