What is the difference between await and yield keywords in JavaScript ?
Last Updated :
01 Apr, 2022
In this article, we will see how Await keyword is different from Yield keywords.
Generator Functions: Functions that can return multiple values at different time interval as per the user demands, and can manage its internal state are generator functions. A function becomes a Generator function if it uses the function* syntax. They are different from normal functions because normal functions completion in a single run, whereas we can pause and resume the generator function.
Note: When generator functions are executed, it returns a new Generator object.
functionality: yield and await can both be used to write asynchronous code that “waits”, which means code that looks as if it was synchronous, even though it really is asynchronous.
await: This is an operator which used to wait for a Promise. In our regular JavaScript code, we use it inside the async function, and it can be used on its own with JavaScript modules.
When we use await keyword in an expression so, the async function execution will get paused until the promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfilment. When resumed, the value of the await expression is that of the fulfilled Promise.
Syntax:
var result = await expression;
expression: A Promise or any value to wait for.
result: contains the value of promise(resolved value).
Example:
JavaScript
<script>
function calculate(y) {
var promise = new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(calculateSquare(y));
}, 3000);
});
return promise;
}
function calculateSquare(y) {
return y * y;
}
async function main() {
/* The expression following the await
the operator is not a Promise, it's
converted to a resolved Promise.*/
// Code will be synchronized
var s1 = await calculate(5);
// After 3 sec this line will get execute
console.log("Square of 5 is : " + s1);
var s2 = await calculate(10);
// After 3 sec next this line will get execute
console.log("Square of 10 is : " + s2);
// In last this line will get execute
console.log("End");
}
main()
</script>
(note: run the above code in node.js)
Output:

yield: The yield keyword is used to pause and resume a generator function.
The keyword yield is used to pause the generator function execution, and the final value of the expression will be the value followed by the yield keyword is returned to the generator's caller.
It causes the call to the generator's next() method to return an IteratorResult object with two properties:
- value: It is the value returned by the yield keyword.
- done: it is of boolean type. (if done is false, it means that the generator function has not been fully completed).
Using it, we can create an iterator for traversing data.
Syntax:
var result = yield expression;
expression: the value to return from the generator function.
result: Retrieves the optional value passed to the generator's next() method to resume its execution.
Example:
JavaScript
<script>
function* getValues(start, end) {
while (start <= end) {
yield start;
start++;
}
}
/* Expression */
var it = getValues(5, 10);
/* Generator's next() method return an
IteratorResult object with two
properties: value and done*/
//element : {value : Object ,done : boolean}
var element = it.next();
while (element.done == false) {
console.log(element);
element = it.next();
}
// value:undefined, and done : false
console.log(element);
</script>
(note : run the above code in node.js)
Output:

Similar Reads
What is the difference between every() and some() methods in JavaScript ?
In this article, we will see the difference between every() and some() methods in JavaScript. Array.every() methodThe Array.every() method in JavaScript is used to check whether all the elements of the array satisfy the given condition or not. The output will be false if even one value does not sati
3 min read
What is the difference between call and apply in JavaScript ?
JavaScript call() Method: It calls the method, taking the owner object as an argument. The keyword this refers to the 'owner' of the function or the object it belongs to. We can call a method that can be used on different objects. Syntax: object.objectMethod.call( objectInstance, arguments ) JavaScr
2 min read
What is the difference between â(â¦);â and â{â¦}â in ReactJS ?
When you write JavaScript, you can use either the "(â¦)" or "{â¦}" pattern to define objects. In ReactJS, (...); and {...} are used in different contexts and have different purposes, and are used to denote different types of code structures. What is "(â¦);" in React JS ?In ReactJS, (...); is used to de
5 min read
What's the difference between JavaScript and JScript?
JavaScript: JavaScript is a programming language which is commonly used in wed development. Its code is only run in web browser. JavaScript is one of the core technologies of world wide web along with HTML and CSS. JavaScript was designed by Brendan Eich and it was first appeared in 4 December 1995.
2 min read
What is the yield Keyword in JavaScript?
The yield keyword in JavaScript is used to pause and resume a generator function asynchronously. A generator function works similarly to a normal function, but instead of returning values with return, it uses yield. This allows for the function's execution to be paused and resumed at specific points
2 min read
What is the Difference Between Promises and Observables in Angular ?
Angular is a TypeScript-based Single-Page Application framework used to create dynamic and responsive web applications. It is one of the most popular and robust frameworks. One of its key features is to handle Asynchronous Operations effectively. In Asynchronous, code is not executed sequentially ra
5 min read
Difference Between sleep and yield Method in Java
In java if a thread doesn't want to perform any operation for a particular amount of time then we should go for the sleep() method, which causes the currently executing thread to stop for the specified number of milliseconds. Syntax : public static native void sleep( long ms) throws InterruptedExcep
4 min read
Difference between Methods and Functions in JavaScript
Grasping the difference between methods and functions in JavaScript is essential for developers at all levels. While both are fundamental to writing effective code, they serve different purposes and are used in various contexts. This article breaks down the key distinctions between methods and funct
3 min read
Difference Between Symbol.iterator and Symbol.asyncIterator in JavaScript
JavaScript offers powerful mechanisms for handling iteration through objects, especially with the introduction of symbols like Symbol.iterator and Symbol.asyncIterator. These symbols play important roles in defining how objects are iterated over, whether synchronously or asynchronously. In this arti
3 min read
Difference Between JavaScript and jQuery
JavaScript is a programming language used for web development, while jQuery is a library written in JavaScript, simplifying tasks like DOM manipulation, event handling, and AJAX requests, making JavaScript code more concise and readable. JavaScriptJavaScript is a crucial scripting language for enhan
6 min read