How to convert an asynchronous function to return a promise in JavaScript ?
Last Updated :
12 Jan, 2022
In this article, we will learn how to convert an asynchronous function to return a promise in JavaScript.
Approach: You need to first declare a simple function (either a normal function or an arrow function (which is preferred)). You need to create an asynchronous function and then further you need to return the promise as an output from that asynchronous function.
We need to create a function (method), either a simple function or an arrow function (We are analyzing the facts using arrow functions). Create an asynchronous function and then upon calling that function we should return the output in the form of promise.
Let's first understand how to declare a simple arrow function in JavaScript and return the result associated with that function in the console.
Example:
JavaScript
<script>
let name = () =>{
console.log("GeeksforGeeks");
}
name();
</script>
Output:
GeeksforGeeks
Approach:
- We will add async() along with function syntax which will eventually handle all kinds of asynchronous operations and events.
- After adding the async keyword, we will store the results.
- After storing the results we will call the function and see that a promise is returned containing the state (as fulfilled) and value that was associated.
Example 1:
JavaScript
<script>
let name = async () => {
return "GeeksforGeeks";
};
console.log(name());
</script>
Output:
Promise {<fulfilled>: "GeeksforGeeks"}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "GeeksforGeeks"
Example 2: You can also add await keyword and store that result in some variable. This is helpful when we fetch data from the API to wait for data to arrive properly.
JavaScript
<script>
let name = async () => {
let output = await ( "GeeksforGeeks");
return output;
};
console.log(name());
</script>
Output:
Promise {<pending>: "GeeksforGeeks"}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "GeeksforGeeks"
Example 3: To have a look over the result we will use the then() method to print the result.
JavaScript
<script>
let name = async () => {
return "GeeksforGeeks";
};
name().then((value) => {
console.log(value);
});
</script>
Output:
GeeksforGeeks
Approach 2:
- We will use resolve() state of the Promise.
- We will store our result and then using both async keyword (along with function syntax) and await (before storing the result into a variable).
Example 1:
JavaScript
<script>
let name = async() =>{
let output = await Promise.resolve("GeeksforGeeks");
return output;
}
console.log(name());
</script>
Output:
Promise {<pending>: "GeeksforGeeks"}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: "GeeksforGeeks"
Example 2: If you wish to see the result also then as explained in the previous approach, you will use then() method, with the help of which you will see the result in the console.
JavaScript
<script>
let name = async() =>{
let output = await Promise.resolve("GeeksforGeeks");
return output;
}
name().then((result)=>{
console.log(result);
});
</script>
Output:
GeeksforGeeks
Similar Reads
How To Return the Response From an Asynchronous Call in JavaScript? To return the response from an asynchronous call in JavaScript, it's important to understand how JavaScript handles asynchronous operations like fetching data, reading files, or executing time-based actions. JavaScript is a single-threaded nature means it can only handle one task at a time, but it u
3 min read
How to create an Asynchronous function in Javascript? JavaScript is a single-threaded and synchronous language. The code is executed in order one at a time. But Javascript may appear to be asynchronous in some situations. Example: HTML <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title
6 min read
How to chain asynchronous functions in JavaScript ? JavaScript is a single-threaded, asynchronous programming language. Thus, some time-consuming operations like I/O, accessing the database, network calls, etc. are performed asynchronously so that it does not interrupt things in the only JS thread. This can be done by asynchronous code like promises
2 min read
How to Convert Callback to Promise in JavaScript ? Asynchronous programming in JavaScript often involves the use of callbacks. However, callbacks can lead to callback hell and make the code harder to read and maintain. Promises provide a cleaner way to handle asynchronous operations. Converting existing callback-based code to use promises can improv
2 min read
How to call a function that return another function in JavaScript ? The task is to call a function that returns another function with the help of JavaScript is called a Currying function, a function with numerous arguments that can be converted into a series of nesting functions with the help of the currying method. Approach:Define Outer Function:Create an outer fun
2 min read