簡體   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