async function getOrderdata(orderId) {
//await the response of the fetch call
let response = await fetch('http://localhost:3245/api/Orders/' + orderId);
//proceed once the first promise is resolved.
let data = await response.json();
//proceed only when the second promise is resolved
return data;
}
I have the shown code and i want to use getOrderdata function in another js file?
Put the function in a module.exports object and use an async-await
function in the importing file.
// file exporting the function - exp.js
module.exports = {
getOrderdata : async function(orderId){
//await the response of the fetch call
let response = await fetch('http://localhost:3245/api/Orders/' + orderId);
//proceed once the first promise is resolved.
let data = await response.json();
//proceed only when the second promise is resolved
return data;
}
// file importing the async function - imp.js
const { getOrderdata } = require("./exp.js")
;(async () =>{
let msg = await getOrderdata()
console.log(msg)
})();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.