asynchronous function

datajcthemax·2023년 6월 28일

next js

목록 보기
10/10

An asynchronous function is a function in JavaScript that is designed to perform a task not instantaneously or in a sequential manner, but at a later point in time or in the background, without blocking the execution of other code.

In JavaScript, the async keyword is used to declare an asynchronous function. Here's an example:

async function fetchData() {
  //...
}

Inside an async function, you can use the await keyword to pause the execution of the function until a Promise is resolved or rejected. A Promise is an object that represents the eventual completion (or failure) of an asynchronous operation and its resulting value.

Here's an example of an async function that uses await to pause its execution until a fetch operation is complete:

async function fetchData() {
  const response = await fetch('https://api.example.com/data');
  const data = await response.json();
  return data;
}

In this example, the fetchData function does not block the rest of the program while waiting for the fetch operation or the conversion of the response to JSON to complete. Instead, it pauses and resumes execution when the awaited operation is complete, allowing other operations to run in the meantime.

In the context of the code you provided, the getSession and getCurrentUser functions are both asynchronous. They use the await keyword to pause their execution until the getServerSession function and the Prisma findUnique query are complete. This means these functions do not block other operations while waiting for these tasks to finish.

0개의 댓글