DEV Community

Cover image for What is the difference between Generator, and Async,Promise ?
Amrita-padhy
Amrita-padhy

Posted on

What is the difference between Generator, and Async,Promise ?

What is the difference

async/await and yield are two different features in JavaScript that are used for managing asynchronous operations and controlling the flow of execution. They serve different purposes and are often used in different contexts

async/await:
async and await are used to work with Promises and make asynchronous code appear more like synchronous code, which can improve readability and maintainability.

async is used to define a function that returns a Promise. It allows the function to use the await keyword.

await is used inside an async function to pause the execution of the function until a Promise is resolved. This helps in avoiding callback hell and simplifies error handling.
Example Use Cases:
Making API requests, reading/writing to databases, and performing multiple asynchronous operations in a sequence.

async function fetchData() {
  try {
    const response = await fetch('https://example.com/data');
    const data = await response.json();
    return data;
  } catch (error) {
    console.error(error);
  }
}
Enter fullscreen mode Exit fullscreen mode

In the example above, fetchData is an async function that uses await to fetch data from a remote server.

yield:
yield is used in the context of generators. A generator is a special type of function that can pause and resume its execution. yield is used to produce values one at a time and can be used to implement custom iteration and control flow.
Example Use Cases: Implementing custom iteration over data, managing complex sequences of events or states, and pausing/resuming execution in specific pattern

function* generateNumbers() {
    yield 1;
    yield 2;
    yield 3;
  const generator = generateNumbers();
  console.log(generator.next()); //{value: 1,done:false}
  console.log(generator.next()); //{value: 2,done:false}
  console.log(generator.next()); //{value: 3,done:false}
console.log(generator.next()//{value: undefine,done:true}

Enter fullscreen mode Exit fullscreen mode

In the example above, generateNumbers is a generator function, and yield is used to produce values one by one when calling generator.next().

What is a Generator function in javascript

Generator functions are an asynchronous programming solution provided by ES6, and their syntactic behavior is completely different from traditional functions. Generator functions bring JavaScript asynchronous programming to a whole new level.

Similar to the function declaration, the difference is that there is an asterisk between the function keyword and the function name, and the yield expression is used inside the function body to define different internal states.

export default function App() {
  function* gen(x) {
    const y = yield x + 6;
    return y;
  }
 const generator = gen(2);
console.log(generator.next());//{value: 8,done:false}
console.log(generator.next());//{value:undefined,done:true} 
Enter fullscreen mode Exit fullscreen mode

In summary, you would choose between async/await and yield based on your specific needs and the nature of the asynchronous operations you are dealing with. If you want to work with Promises and simplify your asynchronous code, you'd choose async/await. If you need to create custom iteration patterns or control the flow of execution, you'd choose yield within generator functions. The choice depends on the problem you're trying to solve and the context in which you're working.

Top comments (0)