如何使用胖箭头表示法将异步功能编写为模块功能

时间:2018-09-28 06:04:22

标签: javascript async-await node-modules

我想编写一个类似于以下内容的模块

应该可以通过“ Server.doBackup”调用它,并使用 fat箭头符号 async-await 函数

有什么想法吗? 您可以在下面提供我的摘录的更正版本吗?

const Server = {
  config: {
    documents: ['DEFAULT', 'KEYS'],
    exportpath: 'data/exportFromCosmos/',
    uploadpath: 'data/uploadToAzureBlob/',
    crosscheckFile: 'data/crosscheckFile.txt'
  },
  doBackup: () => async {
    let prepareFolders = await Folders.prepare(Server.config, resolve)
    let downloadDB_DEFAULT = await Database.downloadDocumentsOfType_DEFAULT()
    let downloadDB_KEYS = await Database.downloadDocumentsOfType_KEYS()
    let zipDocuments = await Documents.zip(Server.config)
  }
}

module.exports = Server

1 个答案:

答案 0 :(得分:3)

const Server = {
  config: {
    documents: ['DEFAULT', 'KEYS'],
    exportpath: 'data/exportFromCosmos/',
    uploadpath: 'data/uploadToAzureBlob/',
    crosscheckFile: 'data/crosscheckFile.txt'
  },
  doBackup: async () =>  {
    let prepareFolders = await Folders.prepare(Server.config, resolve)
    let downloadDB_DEFAULT = await Database.downloadDocumentsOfType_DEFAULT()
    let downloadDB_KEYS = await Database.downloadDocumentsOfType_KEYS()
    let zipDocuments = await Documents.zip(Server.config)
  }
}

module.exports = Server

//示例代码

async function findName(){
  return {name: "Stack"}
}

let user = {
  getName: async()=>{
    let {name} =  await findName()
    return name
  }
}

user.getName()
  .then((userDetail)=> console.log(userDetail))
  .catch((error) => console.log(error))