Return response from a function that uses setTimeout [closed]

I want to return some data from a function that uses setTimeout and then use the data somewhere else in the program.

I am just getting started with asynchronous JavaScript and stumbled upon a problem early on. I have a simple function that mimics an API call (or something similar) and uses the setTimeout function to return the response after some delay. The function looks like this:

// This function mimics the data fetching process
function getData()
{
    setTimeout(() => {
        return "data";
    }, 2000);
}

Now, I know that the setTimeout function doesn’t belong to the JavaScript runtime and is actually provided by the webAPIs. So, when I do something like const data = getData();, the data variable is initially undefined as the setTimeout function is doing its job in another stack, and that the value is returned only after the main stack becomes empty. So, console.log(data); will log undefined to the console because the value IS in fact undefined at the time of calling. The thing I am not being able to understand is that why did the value of data become undefined even after 2 seconds? I console log it from the console after running the script and still the value of data is undefined.

It somewhat makes sense that as we HAD NO assignment in the main stack when the setTimeout returned the data after 2 seconds. But, what IS returned by the function to the main stack after the 2 seconds delay? Is there no way to use the returned value by my program? And if there is not, how can I work with the async stuffs in JavaScript?

I know that the fetch function to get response from APIs return promises and then I can do then on the response to work with it. But in case of the setTimeout function or something else that doesn’t return a promise, how can I use the return values from such functions?

In case of promises, the value of the variable is pending until the data is not fetched and then, it holds either the resolve or the reject value, but that is limited to functions that return a promise. Is there no way to use other async functions that do not return a promise?

A way to make our function work is to return a promise, and then we could use the then on the data returned to do something with the data, but is that the only way to work with asynchronous JavaScript? Are promises inherent to asynchronous JavaScript? Or are there other ways we could achieve this objective?

New contributor

mukesharyal is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật