繁体   English   中英

稍后我如何在另一个函数中使用 promise 返回的数据

[英]How can i use promise returned data in another function later on

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;
}

我有显示的代码,我想在另一个 js 文件中使用 getOrderdata 函数?

将该函数放在 module.exports 对象中,并在导入文件中使用async-await函数。

// 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)
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM