简体   繁体   English

将承诺代码转换为异步等待和模拟测试用例?

[英]Converting Promises Code to async await and mocking test cases?

How to convert the below function which has nested promise and await to just using await or only using promises ?如何将下面嵌套的 promise 和 await 函数转换为仅使用 await 或仅使用 promises ?

const test = (url, id) => {
  return new Promise((_resolve, _reject) => {
    if (isValidUrl(url)) {
      let storage = new Storage(Indexdb, id);
      const cae = new valueExtract(url);
      cae.fetch()
        .then(data => {
          new zip(data)
            .then(obj => obj.getZip())
            .then(obj => obj.getList())
            .then(list => {
              return new Promise(async (resolve, reject) => {
                try {
                  let sI = storage.connection;
                  await Promise.all(Object.keys(list).map(async (fileName, index) => {
                    let blob = await new FileExtractor(list[fileName]);
                    await sI.setItemForce(
                      fileName,
                      new StoreObject(
                        fileName,
                        'testData',
                        blob
                      ).dataObject
                    )
                  }))
                  _resolve(sI);
                } catch (err) {
                  _reject(err)
                }
              })
            })
            .catch(err => _reject(err))
        })
        .catch(err => _reject(err))
    } else {
      _reject('Invalid URL')
    }
  })
};

I was unable to do the same this what i tried but it never resolves我无法像我尝试的那样做同样的事情,但它从未解决

const test = async (url, id) => {
  if (isValidUrl(url)) {
    try {
      let storage = new Storage(Indexdb, id);
      const cae = new valueExtract(url);
      const data = await cae.fetch();
      return new ZIPExtractor(data)
        .then(obj => obj.getZip())
        .then(obj => obj.getList())
        .then(list => {
          return async (resolve, reject) => {
            try {
              let sI = storage.connection;
              await Promise.all(Object.keys(list).map(async (fileName, index) => {
                let blob = await new FileExtractor(list[fileName]);
                await sI.setItemForce(
                  fileName,
                  new StoreObject(
                    fileName,
                    'testData',
                    blob
                  ).dataObject
                )
              }))
            } catch (err) {
              throw new Error(err)
            }
          }
        })
        .catch(err => _reject(err))
    } catch (e) {
      throw new Error('Invalid URL')
    }
  }
};

Also how do we write test case for these kind of a function so that we need not pass in actual network url and mock in jest.还有我们如何为这些类型的函数编写测试用例,这样我们就不需要传入实际的网络 url 并在玩笑中模拟。

It should fulfill, but with the async (resolve, reject) { … } that you return .应该实现,但是使用async (resolve, reject) { … }return You never should've used this in the first place, you can just omit it:你一开始就不应该使用它,你可以省略它:

const test = async (url, id) => {
  if (!isValidUrl(url)) {
    throw new Error('Invalid URL')
  }
  const storage = new Storage(Indexdb, id);
  const cae = new valueExtract(url);
  const data = await cae.fetch();
  const obj = await new ZIPExtractor(data); // shudder. A constructor should never return a promise
  const zip = await obj.getZip();
  const list = await zip.getList();
  const sI = storage.connection;
  await Promise.all(Object.keys(list).map(async (fileName, index) => {
    const blob = await new FileExtractor(list[fileName]);
    const store = new StoreObject(fileName, 'testData', blob);
    await sI.setItemForce(fileName, store.dataObject);
  }));
  return sI; // or something?
}

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

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