将Promises Code转换为async等待和模拟测试用例? [英] Converting Promises Code to async await and mocking test cases?

查看:62
本文介绍了将Promises Code转换为async等待和模拟测试用例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将嵌套了promise和await的以下函数转换为仅使用await或仅使用promise?

How to convert the below function which has nested promise and await to just using await or only using 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')
    }
  }
};

我们还应该如何为此类函数编写测试用例,以使我们无需传入实际的网络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.

推荐答案

应该完成,但您必须return使用async (resolve, reject) { … }.您永远不应该首先使用它,您可以忽略它:

It should fulfill, but with the async (resolve, reject) { … } that you 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?
}

这篇关于将Promises Code转换为async等待和模拟测试用例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆